EF6 和多种配置(SQL Server 和 SQL Server Compact) [英] EF6 and multiple configurations (SQL Server and SQL Server Compact)

查看:33
本文介绍了EF6 和多种配置(SQL Server 和 SQL Server Compact)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:问题已解决,请参阅此问题的结尾.

Update: Problem solved, see end of this question.

问题:

如果我们在同一个 AppDomain 中同时使用了 SQL Server 和 SQL Server CE,我们正在尝试使用实体框架 6 和基于代码的配置.

We are trying to use Entity Framework 6 and code-based configuration in a scenario were we have use both a SQL Server and SQL Server CE in the same AppDomain.

这个非常简单的场景似乎不受设计"支持.来自 EF 团队:

This quite simple scenario seems not to be supported "by design". From the EF team:

注意:我们不支持使用多个配置类同一个 AppDomain.如果你使用这个属性来设置不同的两个上下文的配置类会抛出异常.

Note: We do not support having multiple configuration classes used in the same AppDomain. If you use this attribute to set different configuration classes for two contexts an exception will be thrown.

此处的更多信息:基于代码的配置 (Codeplex)

问题:

我们如何从这里前进?任何帮助将不胜感激!是否有更灵活的方式将配置连接到上下文而不是 AppDomain?

How do we move forward from here? Any help would be greatly appreciated! Is there a more flexible way to connect a configuration to a context instead of an AppDomain?

(我们的上下文类位于不同的程序集中.我们尝试了 DbConfigurationType 属性,但问题出在 EF 本身)

(Our context classes are located in different assemblies. We have tried the DbConfigurationType attribute but the problem is EF itself)

配置文件:

普通SQL服务器的配置

Configuration for normal SQL server

public class EfConfiguration : DbConfiguration
{
    public EfConfiguration()
    {
        SetProviderServices(
            SqlProviderServices.ProviderInvariantName, 
            SqlProviderServices.Instance);

        SetDefaultConnectionFactory(new SqlConnectionFactory());
    }
}

SQL Server Compact Edition 的配置

Configuration for SQL Server Compact Edition

public class EfCeConfiguration : DbConfiguration
{
    public EfCeConfiguration()
    {
        SetProviderServices(
            SqlCeProviderServices.ProviderInvariantName,
            SqlCeProviderServices.Instance);

        SetDefaultConnectionFactory(
            new SqlCeConnectionFactory(SqlCeProviderServices.ProviderInvariantName));
    }
}

更新:

我们得到的错误是:

System.TypeInitializationException : 类型初始值设定项'MyProject.Repositories.Base.DataContext'抛出异常.----> System.InvalidOperationException :一个已设置EfCeConfiguration"的实例,但未设置此类型在与DataContext"上下文相同的程序集中发现.任何一个将 DbConfiguration 类型放在与 DbContext 相同的程序集中类型,在 DbContext 类型上使用 DbConfigurationTypeAttribute 来指定 DbConfiguration 类型,或设置 DbConfiguration 类型配置文件.请参阅 http://go.microsoft.com/fwlink/?LinkId=260883 了解更多信息.

System.TypeInitializationException : The type initializer for 'MyProject.Repositories.Base.DataContext' threw an exception. ----> System.InvalidOperationException : An instance of 'EfCeConfiguration' was set but this type was not discovered in the same assembly as the 'DataContext' context. Either put the DbConfiguration type in the same assembly as the DbContext type, use DbConfigurationTypeAttribute on the DbContext type to specify the DbConfiguration type, or set the DbConfiguration type in the config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.

UPDATE 2,解决方案如上所述,我们只能有一种配置.这是一个问题,因为 Sql 和 SqlCe 使用不同的提供程序.如果我们使用SetDefaultConnectionFactory"来适应一种类型的数据库,另一种就会失败.

UPDATE 2, the solution As described above, we can only have one configuration. This is a problem since Sql and SqlCe uses different providers. If we use "SetDefaultConnectionFactory" to fit one type of database, the other will fail.

相反,按照下面标记为答案的帖子中所述,将连接提供到上下文中.一旦您始终使用连接而不是连接字符串初始化上下文,您就可以开始了.您可以从配置中删除 SetDefaultConnectionFactory 调用.我们只使用下面的代码来配置 SqlCe 上下文,没有配置 Sql 上下文.

Instead, supply the connection into the context as described in the post marked as answer below. Once you always initialize the context with a connection as opposed to a connectionstring you are good to go. You can remove the SetDefaultConnectionFactory call from the configuration. We're using only the code below for configuring the SqlCe Context and no configuration for the Sql Context.

  public class CommonEfConfiguration : DbConfiguration
    {
        public CommonEfConfiguration()
        {
            // EF does not know if the ce provider by default,
            // therefore it is required to be informed about it.
            // The connection factories are not necessary since the connection
            // is always created in the UnitOfWork classes
            SetProviderServices(SqlCeProviderServices.ProviderInvariantName, SqlCeProviderServices.Instance);
        }
    }

推荐答案

based On Error details:您是否已经尝试告诉 EF 配置类的位置?

based On Error details: Did you already try tell EF where the config class is found?

[DbConfigurationType("MyNamespace.MyDbConfiguration, MyAssemblyFullyQualifiedName")]
public class MyContextContext : DbContext
{
}

如果无法解决,请查看替代方案

If that cant be made work, then see alternative

使用带有构造函数 DbConnection 的 Context

Use the Context with constructor DbConnection

public class MYDbContext : DbContext {
     // MIgration parameterless constructor is managed in  MyMigrationsContextFactory 

    public MyDbContext(string connectionName) : base(connectionName) { } // no this

    public MYDbContext(DbConnection dbConnection, bool contextOwnsConnection)  // THIS ONE
        : base(dbConnection, contextOwnsConnection) {  }

然后,您需要为每个提供程序建立一个DBConnection"连接.对于 SQL 服务器

you then need a "DBConnection" connection for each provider. For SQL server

      public DbConnection GetSqlConn4DbName(string dataSource, string dbName) {
        var sqlConnStringBuilder = new SqlConnectionStringBuilder();
        sqlConnStringBuilder.DataSource = String.IsNullOrEmpty(dataSource) ? DefaultDataSource : dataSource;
        sqlConnStringBuilder.IntegratedSecurity = true;
        sqlConnStringBuilder.MultipleActiveResultSets = true;

        var sqlConnFact = new SqlConnectionFactory(sqlConnStringBuilder.ConnectionString);
        var sqlConn = sqlConnFact.CreateConnection(dbName);
        return sqlConn;
    }

重复SqlCe factory,它也可以生成一个DBConnectionSqlCe连接因子创建连接

repeat for SqlCe factory, it can also generate a DBConnection SqlCe connection factor create connection

这篇关于EF6 和多种配置(SQL Server 和 SQL Server Compact)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆