我一直在努力寻找一种以编程方式添加Oracle客户端提供程序名称的方法。我的Context类看起来像这样..
public class SystemFeatureContext : DbContext
{
public SystemFeatureContext(DbContextConfiguration configuration)
{
Database.Connection.ConnectionString = configuration.ConnectionString;
}
}
我通过我的基础架构获得DbContextConfiguration,但试图找出一种分配提供者名称的方法。如果我将整个连接字符串放在我的web.config / app.config中,它在通过基本构造函数传递时工作正常。
<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")
{
}
}
但我的情况要求以编程方式创建上下文。我尝试使用IDBConnectionFactory,但它会抛出错误,因为它没有找到元数据。
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());
}
}
任何帮助都将不胜感激。