相同的应用程序,不同的数据库:实体框架6.X + MySQL + SQL Server [英] Same application, different databases: Entity framework 6.X + MySQL + SQL Server

查看:243
本文介绍了相同的应用程序,不同的数据库:实体框架6.X + MySQL + SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,我使用实体框架迁移(EF 5.0 => EF 6.0)来访问MySql和SQL Server数据库(特定 DbContext 到特定数据库,而不是任何 DbContext 到任何类型的数据库)。

Yesterday I did migration (EF 5.0 => EF 6.0) of web application that uses entity framework to reach MySql and SQL Server databases (particular DbContext to particular databases, NOT any DbContext to any type of database).

编译时间完成没有任何问题,运行时面临我有例外:

Compile time things were done without any issues, run-time faced me with exception:


在发现MySqlEFConfiguration类型之前,默认的DbConfiguration实例被Entity Framework使用。

The default DbConfiguration instance was used by the Entity Framework before the 'MySqlEFConfiguration' type was discovered.

上下文中的 [DbConfigurationType(typeof(MySqlEFConfiguration))] 属性似乎已被忽略在运行时,因为上下文在外部程序集(?)中,而使用的 DbConfiguration 代替应用程序域是全局的,而不是上下文(?)的特定内容。

The [DbConfigurationType(typeof(MySqlEFConfiguration))] attribute on the context appears to have been ignored at run time because the context is in an external assembly(?) and the DbConfiguration used instead is global to the application domain, not the specific to the context(?)."

我尝试了不同的方法来解决它,然后google了, - 惊喜 - 找不到工作的解决方案。

I tried different approaches to fix it, then googled it and - surprise - find no working solution.

看起来这里描述的情况很好, http://forums.mysql.com/ read.php?174,614148,614148 仍然没有改变,或者我错过了一些明显的事情。

Looks like situation described well formed here http://forums.mysql.com/read.php?174,614148,614148 still not changed, or I missed some obvious things.

任何反馈将不胜感激。

Any feedback will be appreciated.

提前谢谢!

详细说明: strong>

输入(简化):
- ASP.NET Web应用程序

Input (simplified): - ASP.NET Web Application


  • 实体框架6.1.1实施的数据访问层

  • Data access layer implemented over Entity Framework 6.1.1

实体框架提供者:


  • System.Data.SqlClient 6.1.1

  • System.Data.SqlClient 6.1.1

MySql.Data.MySqlClient 6.9.4


MySql.Data.MySqlClient 6.9.4

MY_SqlContext,模型第一个概念,针对MY SQL Server数据库

MY_SqlContext, model first concept, targeted to MY SQL Server database

Ms_SqlContext,数据库第一个概念,针对MS SQL Server数据库

Ms_SqlContext, database first concept, targeted to MS SQL Server database

根据通用文档的实体框架6和MySql连接器/ Net文档
http://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html ),
MY_SqlContext需要MySqlEFConfiguration要应用。

According generic documentation of Entity Framework 6 and MySql Connector/Net documentation (http://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html), MY_SqlContext requires MySqlEFConfiguration to be applied.

根据上述两个文件,有三个选择。
所有三个被尝试并失败。

According both documentations, refered above, there are three options to do that. All three was tried and failed.

选项1 :添加DbConfigurationTypeAttribute [DbConfigurationType(typeof MySqlEFConfiguration))] 到MY_SqlContext类

Option 1: Adding the DbConfigurationTypeAttribute [DbConfigurationType(typeof(MySqlEFConfiguration))] to MY_SqlContext class

适当的Web.config段:

Appropriate Web.config segments:

<connectionStrings>
    <add name="MY_SqlContext"
         connectionString="server=.;User Id=root;password=...;Persist Security Info=True;database=MySqlDb;Use Compression=False;Use Old Syntax=False"
         providerName="MySql.Data.MySqlClient" />
    <add name="Ms_SqlContext"
         connectionString="metadata=res://*/Ms_SqlContext.csdl|res://*/Ms_SqlContext.ssdl|res://*/Ms_SqlContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=MsSqlDb;User ID=appuser;Password=...&quot;"
         providerName="System.Data.EntityClient" />
</connectionStrings>

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
        <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,  MySql.Data.Entity.EF6" />
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>

应用程序启动和Web请求开始处理后:

After application starts and web requests begin processing:

Ms_SqlContext工作正常,但尝试创建一个MY_SqlContext实例,我收到例外:

Ms_SqlContext works fine but trying to create a MY_SqlContext instance, I get the exception:


实体使用了默认的DbConfiguration实例发现MySqlEFConfiguration类型之前的框架。在使用任何实体框架功能之前,必须先在应用程序启动时设置MySqlEFConfiguration实例,或者必须在应用程序的配置文件中注册。请参阅... LinkId = 260883了解更多信息。

The default DbConfiguration instance was used by the Entity Framework before the 'MySqlEFConfiguration' type was discovered. An instance of 'MySqlEFConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file. See ...LinkId=260883 for more information."

选项2 :调用DbConfiguration.SetConfiguration新的MySqlEFConfiguration())在应用程序启动时

Option 2: Calling DbConfiguration.SetConfiguration(new MySqlEFConfiguration()) at the application startup

适当的Web.config段(实际上与选项1相同):

Appropriate Web.config segments (same as Option 1, actually):

<connectionStrings>
    <add name="MY_SqlContext"
         connectionString="server=.;User Id=root;password=...;Persist Security Info=True;database=MySqlDb;Use Compression=False;Use Old Syntax=False"
         providerName="MySql.Data.MySqlClient" />
    <add name="Ms_SqlContext"
         connectionString="metadata=res://*/Ms_SqlContext.csdl|res://*/Ms_SqlContext.ssdl|res://*/Ms_SqlContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=MsSqlDb;User ID=appuser;Password=...&quot;"
         providerName="System.Data.EntityClient" />
</connectionStrings>

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
        <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,  MySql.Data.Entity.EF6" />
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>

添加到Global.asax.cs中的代码:
private void Application_Start(object sender, EventArgs e)
{
DbConfiguration.SetConfiguration(new MySqlEFConfiguration());
...

Code, added to Global.asax.cs: private void Application_Start(object sender, EventArgs e) { DbConfiguration.SetConfiguration(new MySqlEFConfiguration()); ...

应用程序启动后,Web请求开始处理尝试创建Ms_SqlContext实例,我收到例外:

After application starts and web requests begin processing trying to create a Ms_SqlContext instance, I get the exception:


设置了MySqlEFConfiguration的一个实例,但是在Ms_SqlContext上下文的同一程序集中未发现此类型。将DbConfiguration类型放在与DbContext类型相同的程序集中,在DbContext类型上使用DbConfigurationTypeAttribute来指定DbConfiguration类型,或者在配置文件中设置DbConfiguration类型。请参阅... LinkId = 260883了解更多信息。

An instance of 'MySqlEFConfiguration' was set but this type was not discovered in the same assembly as the 'Ms_SqlContext' 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 ...?LinkId=260883 for more information.

选项3 :将DbConfiguration类型设置为配置文件

Option 3: Set the DbConfiguration type in the configuration file

适当的Web.config段

Appropriate Web.config segments

<connectionStrings>
    <add name="MY_SqlContext"
         connectionString="server=.;User Id=root;password=...;Persist Security Info=True;database=MySqlDb;Use Compression=False;Use Old Syntax=False"
         providerName="MySql.Data.MySqlClient" />
    <add name="Ms_SqlContext"
         connectionString="metadata=res://*/Ms_SqlContext.csdl|res://*/Ms_SqlContext.ssdl|res://*/Ms_SqlContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=MsSqlDb;User ID=appuser;Password=...&quot;"
         providerName="System.Data.EntityClient" />
</connectionStrings>

<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
        <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,  MySql.Data.Entity.EF6" />
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>

应用程序启动和Web请求开始处理后:
...
Ms_SqlContext实例被创建,但在第一次查询执行期间,我得到了异常:

After application starts and web requests begin processing: ... Ms_SqlContext instance is created, but during first query execution, I get the exception:


EntityException:{底层提供程序在Open上失败。

EntityException: {"The underlying provider failed on Open."}

InnerException:{无法连接到任何指定的MySQL主机。}

InnerException: {"Unable to connect to any of the specified MySQL hosts."}

所以,Ms_SqlContext得到明显错误的MySql配置。

So, Ms_SqlContext get MySql configuration that is obviously wrong.

推荐答案

所以最终的解决方案是: >

So, final solution is:


  1. 使用二十一点和妓女创建自己的DbConfiguration继承人:

  1. Create own DbConfiguration successor with blackjack and hookers:

    public class MultipleDbConfiguration : DbConfiguration
    {
        #region Constructors 

        public MultipleDbConfiguration()
        {
            SetProviderServices(MySqlProviderInvariantName.ProviderName, new MySqlProviderServices());
        }

        #endregion Constructors

        #region Public methods 

        public static DbConnection GetMySqlConnection(string connectionString)
        {
            var connectionFactory = new MySqlConnectionFactory();

            return connectionFactory.CreateConnection(connectionString);
        }

        #endregion Public methods
    }   


  • 使用MultipleDbConfiguration标记Ms_SqlContext(并且不用DbContext执行任何操作)

  • Mark Ms_SqlContext with MultipleDbConfiguration (and do nothing else with that kind of DbContext)

        [DbConfigurationType(typeof(MultipleDbConfiguration))]
        partial class Ms_SqlContext
        {
        }
    


  • 使用MultipleDbConfiguration标记Ms_SqlContext,并调用MY_SqlContext(string nameOrConnectionString),调用MultipleDbConfiguration.GetMySqlConnection(nameOrConnectionString)

  • Mark Ms_SqlContext with MultipleDbConfiguration, and ajust MY_SqlContext(string nameOrConnectionString) with call MultipleDbConfiguration.GetMySqlConnection(nameOrConnectionString)

        [DbConfigurationType(typeof(MultipleDbConfiguration))]
        partial class MY_SqlContext : DbContext
        {
                    public MY_SqlContext(string nameOrConnectionString) : base(MultipleDbConfiguration.GetMySqlConnection(nameOrConnectionString), true)
                    {}
        }
    


  • 这是它!!!

  • THAT IS IT!!!

    这篇关于相同的应用程序,不同的数据库:实体框架6.X + MySQL + SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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