Search code examples
c#oracle-database.net-6.0asp.net-core-scaffolding

EF Core Scaffolding an Oracle Database - Pass OracleConfiguration into the connection string


Here's the situation. I have a .Net Core 6 Console app that reads data from an Oracle database using the following configuration:

var connectionString = "User Id=MY_USER;Password=MY_PASSWORD;Data Source=MY_DATASOURCE;"    

OracleConfiguration.DirectoryServers = "(my.directory.server:123:456)";
OracleConfiguration.DirectoryServerType = "AD"
OracleConfiguration.DefaultAdminContext = "cn=my_cn,dc=my_dc_1,dc=my_dc_2"
OracleConfiguration.NamesDirectoryPath = "(LDAP)"
OracleConfiguration.SqlNetAuthenticationServices = "(NONE,NTS)"

_ = services.AddDbContext<DbContext, MyDbContext>(options =>
{
   _ = options.UseOracle(connectionString);
});

This works prefectly fine.

Now I need to scaffold the said Oracle database using

dotnet-ef dbcontext scaffold "User Id=MY_USER;Password=MY_PASSWORD;Data Source=MY_DATASOURCE;" "Oracle.EntityFrameworkCore" 

This returns the following error ORA-12154: TNS:could not resolve the connect identifier specified

This was expected since I did not include all the custom OracleConfiguration into the connection string passed to dotnet-ef

How can I include those custom OracleConfiguration into the connection string ?


Solution

  • I should have made a more torough search before asking this question but I will share my findings anyhow.

    As it turns out, the server name (or server's ip) should be used, to avoid relying on a TNS resolution that was causing the exception in the first place (ORA-12154: TNS:could not resolve the connect identifier specified).

    dotnet-ef dbcontext scaffold "User Id=MY_USER;Password=MY_PASSWORD;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MY_DB_SERVER_NAME)(PORT=MY_PORT)))(CONNECT_DATA=(SERVICE_NAME=MY_DB_NAME)));" "Oracle.EntityFrameworkCore"