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.