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

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

问题描述

更新:问题解决,见此问题的结尾。





我们试图在一个场景中使用Entity Framework 6和基于代码的配置,我们在同一个 AppDomain中使用SQL Server和SQL Server CE CE



这个很简单的情况似乎不支持按设计。从EF团队:


注意:我们不支持在
中使用同一个AppDomain中的多个配置类。如果使用此属性为两个上下文设置不同的
配置类,将抛出异常。


基于代码的配置(Codeplex)



问题



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



我们尝试了 DbConfigurationType 属性,但问题是EF本身)



配置文件:



正常SQL服务器的配置

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

SetDefaultConnectionFactory(new SqlConnectionFactory());
}
}

SQL Server Compact Edition的配置

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

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

UPDATE: p>

我们得到的错误是:


System.TypeInitializationException:
'MyProject.Repositories.Base.DataContext'
抛出异常。 ----> System.InvalidOperationException:设置了'EfCeConfiguration'的
实例,但是在与DataContext上下文相同的程序集中发现该类型不是

将DbConfiguration类型与DbContext
类型放在同一个程序集中,对DbContext类型使用DbConfigurationTypeAttribute类型为
指定DbConfiguration类型,或者将
中的DbConfiguration类型设置为config文件。有关
的信息,请参见 http://go.microsoft.com/fwlink/?LinkId=260883 信息。


UPDATE 2,解决方案
如上所述,组态。这是一个问题,因为Sql和SqlCe使用不同的提供程序。如果我们使用SetDefaultConnectionFactory来匹配一种类型的数据库,那么另一个将会失败。



而是将连接提供给上下文。一旦你总是用一个连接初始化上下文,而不是一个连接字符串你很好去。您可以从配置中删除SetDefaultConnectionFactory调用。我们只使用下面的代码配置SqlCe上下文,没有配置Sql上下文。

  public class CommonEfConfiguration:DbConfiguration 
{
public CommonEfConfiguration()
{
// EF不知道ce provider是否默认,
//因此需要通知它。
//连接工厂不是必需的,因为连接
//总是在UnitOfWork类中创建
SetProviderServices(SqlCeProviderServices.ProviderInvariantName,SqlCeProviderServices.Instance);
}
}


解决方案

:根据错误的详细信息:
你已经尝试告诉EF找到配置类吗?

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

如果无法工作,请参阅替代



使用构造函数DbConnection上下文

  public class MYDbContext:DbContext {
//在MyMigrationsContextFactory中管理MIgration无参构造函数

public MyDbContext(string connectionName):base(connectionName) {} //没有这个

public MYDbContext(DbConnection dbConnection,bool contextOwnsConnection)//这一个
: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
< a href =http://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.sqlceconnectionfactory.createconnection%28v=vs.113%29.aspx =nofollow> SqlCe连接因素创建连接


Update: Problem solved, see end of this question.

The problem:

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.

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

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.

More information here: Code-based Configuration (Codeplex)

The question:

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?

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

Configuration files:

Configuration for normal SQL server

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

        SetDefaultConnectionFactory(new SqlConnectionFactory());
    }
}

Configuration for SQL Server Compact Edition

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

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

UPDATE:

The error which we get is:

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, 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.

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

解决方案

EDIT: 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

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

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

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

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

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