实体框架6 -多个DefaultConnectionFactory

本文关键字:多个 DefaultConnectionFactory 框架 实体 | 更新日期: 2023-09-27 18:15:09

我创建了2个上下文。SQLCE上下文和MYSQL上下文

public class SistemaContext : Sistema.Common.Repository.DataContext
{
    static SistemaContext()
    {
        DbConfiguration.SetConfiguration(new DbConfigurationBase(Sistema.DataAccess.ConnectionStringFactory.DBType.MYSQL));
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<SistemaContext, Sistema.DataAccess.Migrations.Configuration>());
    }
    public SistemaContext()
        : base(GetConnectionString())
    {
    }
    private static string GetConnectionString()
    {
        return ConnectionStringFactory.ReturnConnectionString(ConnectionStringFactory.DBType.MYSQL);
    }
...

public class SistemaContext2 : Sistema.Common.Repository.DataContext
{
    static SistemaContext2()
    {
        DbConfiguration.SetConfiguration(new DbConfigurationBase(Sistema.DataAccess.ConnectionStringFactory.DBType.SQLCE));
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<SistemaContext2, Sistema.DataAccess.Migrations.Configuration2>());
    }
    public SistemaContext2()
        : base(GetConnectionString())
    {
    }
    private static string GetConnectionString()
    {
        return ConnectionStringFactory.ReturnConnectionString(ConnectionStringFactory.DBType.SQLCE);
    }
...

我为它创建了2个配置。

public class DbConfigurationBase : DbConfiguration
{
    public DbConfigurationBase(Sistema.DataAccess.ConnectionStringFactory.DBType type)
    {
        if (type == ConnectionStringFactory.DBType.MYSQL)
        {
            //<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0" />
            string invariantname = MySql.Data.Entity.MySqlProviderInvariantName.ProviderName;//MySql.Data.MySqlClient
            SetDefaultConnectionFactory(new MySql.Data.Entity.MySqlConnectionFactory());
            //this.AddDependencyResolver(new SingletonDependencyResolver<IDbConnectionFactory>(new MySql.Data.Entity.MySqlConnectionFactory()));
            SetProviderFactory(invariantname, new MySql.Data.MySqlClient.MySqlClientFactory());
            SetProviderServices(invariantname, new MySql.Data.MySqlClient.MySqlProviderServices());
        }
        else if(type == ConnectionStringFactory.DBType.SQLCE)
        {
            //<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0" />
            const string invariantname = "System.Data.SqlServerCe.4.0";//System.Data.SqlServerCe.4.0
            SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlCeConnectionFactory(invariantname));
            //this.AddDependencyResolver(new SingletonDependencyResolver<IDbConnectionFactory>(new System.Data.Entity.Infrastructure.SqlCeConnectionFactory(invariantname)));
            SetProviderFactory(invariantname, new System.Data.SqlServerCe.SqlCeProviderFactory());
            SetProviderServices(invariantname, SqlCeProviderServices.Instance);
        }
    }
}
我App.config

:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="ConnMYSQL" value="EAb4YPpPv+VpS9RTnIn9TuPl2GiDxgnqWBwSJ5RH+8ANZYaXEUUKzo/shyUDUM4GQrpKxBXUC9syTTr6B2ho/Q==" />
    <add key="ConnSQLCE" value="bQUToSc3LGkAI8GQGq3tmbbsGOonnbfXsV9kD1U0RT9wIHRoxHNmswPpEUhpo0dEXiJRhCwpa48o328gvX9xxw==" />
  </appSettings>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.Unity" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="3.5.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Data" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
      <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient"/>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0" />
      <remove invariant="System.Data.SqlServerCe.4.0" />
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
  </system.data>
</configuration>

但是当我调试它时,上下文是在同一个数据库提供程序上创建的。只有MYSQL,或只有SQLCE提供者在2上下文中。

如何为确定的上下文正确设置提供者?

我尝试AddDependencyResolver,但没有成功。

实体框架6 -多个DefaultConnectionFactory

你需要为两者创建一个connectionString。

这是从我的app.conf文件中复制的,所以你必须调整它以适应你自己的。可能不仅仅是数据库源,用户和传递,还有正确的实体框架和东西。

<connectionStrings>
     <add name="configuratieEntities"
          connectionString="metadata=res://*/Model.configuratie.csdl|res://*/
              Model.configuratie.ssdl|res://*/Model.configuratie.msl;
              provider=System.Data.SqlClient;provider connection 
              string=&quot;
              data source=***Your database***;
              initial catalog=*Your schema*;
              persist security info=True;user id=***Your user***;
              password=***Your password***;
              multipleactiveresultsets=True;application 
              name=EntityFramework&quot;" 
          providerName="System.Data.EntityClient" />
</connectionStrings>

所以你知道我的提供者是什么,你可以相应地调整你的连接字符串

<providers>
      <provider invariantName="System.Data.SqlClient" 
                type="System.Data.Entity.SqlServer.SqlProviderServices, 
                      EntityFramework.SqlServer" />
</providers>