3

I have been struggling to figure out a way to add the Oracle client provider name pro-grammatically. My Context class looks like this..

public class SystemFeatureContext : DbContext
{

public SystemFeatureContext(DbContextConfiguration configuration)
    {
        Database.Connection.ConnectionString = configuration.ConnectionString;
    }
} 

I get the DbContextConfiguration through my infrastructure, but trying to figure out a way to assign the provider name. If I put the whole connection string in my web.config/app.config it just works fine when passed thru the base constructor.

<connectionStrings>
<add name="Context" connectionString="DATA SOURCE=abcd;USER ID=xxxx;PASSWORD=xxxxx$1;PERSIST SECURITY INFO=True;POOLING=False;" providerName="Oracle.DataAccess.Client" />

public class SystemFeatureContext : DbContext
{

public SystemFeatureContext():base("Name=Context")
    {
    }
} 

But my situation demand to create the context programmatically. I tried implmenting IDBConnectionFactory but it throws error that it did not find metadata.

public class OracleConnctionFactory : IDbConnectionFactory
{
    private readonly string oracleConnString;
    public OracleConnctionFactory(string connString)
    {
        oracleConnString = connString;
    }
    public System.Data.Common.DbConnection CreateConnection(string oracleConnString)
    {
       oracleConnString = this.oracleConnString;
        var connectionStringBuilder = new EntityConnectionStringBuilder();
        connectionStringBuilder.ProviderConnectionString = oracleConnString;
        connectionStringBuilder.Provider = "Oracle.DataAccess.Client";
        connectionStringBuilder.Metadata = "";
        return new EntityConnection(connectionStringBuilder.ToString());

    }
}

Any help would be appreciated.

Raj Yennam
  • 51
  • 4

0 Answers0