c# - EF6 和多种配置(SQL Server 和 SQL Server Compact)

标签 c# entity-framework configuration entity-framework-6

更新:问题已解决,见本题末尾。

问题:

如果我们在同一个 AppDomain 中同时使用 SQL Server 和 SQL Server CE,我们正在尝试使用 Entity Framework 6 和基于代码的配置。

“设计”似乎不支持这种非常简单的场景。来自 EF 团队:

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.

更多信息在这里:Code-based Configuration (Codeplex)

问题:

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

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

配置文件:

普通SQL服务器的配置

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

        SetDefaultConnectionFactory(new SqlConnectionFactory());
    }
}

SQL Server 精简版的配置

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

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

更新:

我们得到的错误是:

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.

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

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

  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);
        }
    }

最佳答案

编辑:基于错误详情: 您是否已尝试告诉 EF 在何处找到配置类?

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

如果不能解决这个问题,那就看看替代方案

在构造函数 DbConnection 中使用 Context

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 服务器

      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工厂,它也可以生成一个DBConnection SqlCe connection factor create connection

关于c# - EF6 和多种配置(SQL Server 和 SQL Server Compact),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20354083/

相关文章:

asp.net-mvc - 无需 ORM 即可查看 MVC 4 强类型数据

java - 如何在 GlassFish 中使用属性文件

java - Lucene-Appengine 的 SegmentIndexInput readByte 方法中的 NullPointerException

c# - Entity Framework 中的删除顺序不可预测

grails - Grails抛出H2 WebServlet加载错误

c# - HttpContentExtensions.ReadAsAsync 错误

c# - MVC 6 WebAPI FromServices 属性无法编译

c# - 使用 Oracle 零日期

c# - 大理石图

c# - 无法在配置中设置 ProviderService