我创建了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提供程序。

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

我尝试了AddDependencyResolver,但没有成功。

最佳答案

您需要为我都创建一个connectionString。

这是从我的app.conf文件中复制的,因此您必须对其进行调整以适合您自己的情况。可能不仅是数据库的源,用户和密码,而且还有正确的enityframework和其他东西。

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


希望这对您有所帮助。

关于c# - Entity Framework 6-多个DefaultConnectionFactory?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24996931/

10-11 02:11