使用来自SQL Server和Oracle的数据

时间:2016-09-06 19:06:01

标签: asp.net-mvc entity-framework

我正在努力让我的应用程序使用来自SQL Server数据库和Oracle数据库的数据。我有一个DbContext类,每个类都有一个相应的DbConfiguration类。

SQL Server:

public class SqlDbConfig : DbConfiguration
{
    public SqlDbConfig()
    {
        this.SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlConnectionFactory());
    }
}

[DbConfigurationType(typeof(SqlDbConfig))]
public partial class VcisAppContext : DbContext
{
    public VcisAppContext(string nameOrConnectionString) : base(nameOrConnectionString)
    { }
 }

甲骨文:

public class OracleDbConfig : DbConfiguration
{
    public OracleDbConfig()
    {
           SetDefaultConnectionFactory(new OracleConnectionFactory());
           SetProviderServices("Oracle.ManagedDataAccess.Client",    EFOracleProviderServices.Instance);            
           SetProviderFactory("Oracle.ManagedDataAccess.Client", new OracleClientFactory());
    }
}

[DbConfigurationType(typeof(OracleDbConfig))]
public partial class VcisDataStoreContext : DbContext
{
        // first try
        // public VcisDataStoreContext(string nameOrConnectionString) : base(new OracleConnection(nameOrConnectionString), true) { }
        // second try
        // public VcisDataStoreContext(string nameOrConnectionString) : base(nameOrConnectionString) { }
        //let's try passing a connection 
        public VcisDataStoreContext(DbConnection dbConnection, bool contextOwnsConnection)
        : base(dbConnection, contextOwnsConnection) { }

适配器代码:

public FundAdapter(IOptions<AppSettings> appOptions)
{
        //Create a connection and pass it in
        var connection = new OracleConnection(appOptions.Value.VcisDataStoreConnection);

        var context = new VcisDataStoreContext(connection, true);
        // try passing in a connection string
        //var context = new VcisDataStoreContext(appOptions.Value.VcisDataStoreConnection);
        this.repository = new FundRepository(context);
        this.mapper = Mapper.Instance;
}

首先使用SQL Server DbContext时它会起作用,而Oracle DbContext会失败。如果我不先调用SQL Server DbContext,那么Oracle就可以运行。

是否可以在ASP.NET MVC应用程序中同时使用SQL Server和Oracle?

更新:我看到的错误消息是:

  

无法确定“Oracle.ManagedDataAccess.Client.OracleClientFactory”类型的提供程序工厂的提供程序名称。确保在应用程序配置中安装或注册了ADO.NET提供程序。

当我查看调试器中的上下文对象时,context.Database.DefaultConnection显示:

Data Source=.\\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True;

因此,即使传递了正确的Oracle连接,Context对象仍然有错误的DbFactory

0 个答案:

没有答案