Monday, February 2, 2015

Entity framework 5 can't connect to Oracle DB in Windows 2012 server

Recently I did a project using Entity Framework 5 with Oracle DB.
Only Entity framework 5 is able to connect to Oracle, I'm using Oracle 12c so must use the ODAC 12.10.1.2 for coding.
When running in local, it's working fine.
However when deploying to the server, I encountered this error:

ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA

Reinstalling ODAC doesn't work.
All TNS was updated in tnsnames.ora.
However it seems EF can't locate the TNS in tnsnames.ora.

Changing the connection string to be same like the one in tnsnames.ora doesn't work.

metadata=res://*/Entities.csdl|res://*/Entities.ssdl|res://*/Entities.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string='Data Source=MYDB.WORLD;USER ID=XXX;PASSWORD=YYY;'

After enough searching here and there.. I found a temporary solution, that is to embed the TNS names in the connection string itself.

So my entity framework connection string is now like below:

metadata=res://*/Entities.csdl|res://*/Entities.ssdl|res://*/Entities.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string='Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = MYDBSERVER)(PORT = 1111))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = MYDB)));USER ID=XXX;PASSWORD=YYY;'

Reference : http://nullablecode.com/2013/10/ef-oracle-db-connection-problem-ora-12504/