How to manually set Oracle connection string in DbContext

I have the following connection string:

<add name="DataContext" connectionString="DATA SOURCE=Server; PASSWORD=123;USER ID=SYSTEM" providerName="Oracle.DataAccess.Client"/> 

My business logic determines that I need to manually read the database connection string:

 class MyDbContext: DbContext { public MyDbContext() : base(ConfigurationManager.ConnectionStrings["DataContext"].ConnectionString){} ... } 

It works correctly with Sql Server, but when I go to the Oracle Connection line, it does not work. This is because DbContext is trying to use Oracle ConnectionString to connect to the Sql Server database, because it does not get the provider name.

Does anyone know how to solve this problem?

+6
source share
3 answers

To create a DbContext using Oracle without using WebConfig, your DbContext inheritance should insert Oracle Connection into the base constructor:

 class MyDbContext: DbContext { public MyDbContext() : base(new OracleConnection("DATA SOURCE=Server; PASSWORD=123;USER ID=SYSTEM"){} ... } 
+6
source

I just found a solution after fighting this all day. It seems that the constructor or DbContext will use either a connection string or a connection string name, which is not the same if you pass a connection string, by default it will be equal to SqlClient, which is the only thing enabled by default in .NET

Now, if instead of using the entire connection string, you pass only the name of the connection string, then it will also internally analyze the "providerName" parameter, which, for example, has the assembly name for the Oracle.DataAccess.Client database provider.

Therefore, instead of passing the connection string to the DbContext constructor, simply pass the name of the connection string, for example:

.config file:

 <connectionStrings> <add name="SQLServer" connectionString="Server=localhost; Database=MyDb; User ID=MyUser;Password=MyPwd;Pooling=false" providerName="System.Data.SqlClient" /> <add name="Oracle" connectionString="Data Source=localhost:1521/XE;Persist Security Info=True;User ID=MyUser;Password=MyPwd;" providerName="Oracle.ManagedDataAccess.Client"/> </connectionStrings> <appSettings> <add key="DefaultConnection" value="Oracle" /> </appSettings> 

And in your DbContext:

 public MyDbContext() : base("DefaultConnection") { } 

Thus, you simply configured the configuration key with the name of the connection string to which you want to connect the context and use it in the constructor. If you do this, EF will automatically parse the entire tag of the connection string, not just the value of the connectionString attribute, therefore, it will load the desired provider.

Please note that I am using Oracle.ManagedDataAccess.Client, which is newer and is included in ODAC / ODP.NET v12 and higher. If you are using ODAC 11, you must use Oracle.DataAccess.Client in the provider name.

+3
source

It turned out by specifying the "Default Connection Factory" in web / app.config

 <configuration> ... <entityFramework> <defaultConnectionFactory type="Oracle.ManagedDataAccess.EntityFramework.OracleConnectionFactory, Oracle.ManagedDataAccess.EntityFramework" /> <providers> ... 

https://docs.oracle.com/cd/E63277_01/win.121/e63268/entityCodeFirst.htm

0
source

Source: https://habr.com/ru/post/900070/


All Articles