Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 实体框架一次支持多个ORM数据库吗?_.net_Asp.net Mvc_Entity Framework_Entity Framework 6 - Fatal编程技术网

.net 实体框架一次支持多个ORM数据库吗?

.net 实体框架一次支持多个ORM数据库吗?,.net,asp.net-mvc,entity-framework,entity-framework-6,.net,Asp.net Mvc,Entity Framework,Entity Framework 6,实体框架是否支持多个数据库?我的解决方案中有两个数据库,MySQL和sqlserver。如果我一次运行一个,它是工作的,但同时两个都不工作。下面是代码。在entity frameworkweb config中,我需要为mysql添加另一个设置,但如何添加 <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, Enti

实体框架是否支持多个数据库?我的解决方案中有两个数据库,
MySQL
sqlserver
。如果我一次运行一个,它是工作的,但同时两个都不工作。下面是代码。在entity framework
web config
中,我需要为mysql添加另一个设置,但如何添加

<entityFramework> 
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
  <parameters>
  <parameter value="System.Data.SqlServerCe.4.0" />
  </parameters>
  </defaultConnectionFactory>
  <providers>
  <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>
</entityFramework>

创建一个包含3个项目的解决方案: MsSQLContext MySQLContext 业务逻辑

MsSQLContext项目

安装EF并设置以下连接字符串

MySQLContext项目

安装EF并设置默认情况下提供的Ms SQL连接字符串。给它起个名字,你会得到类似的东西,如下所示:

  <connectionStrings>
    <clear />
    <add name="MSSQLContext" connectionString="...valid conn string..." />
  </connectionStrings>
业务逻辑项目

安装EF并将这两个连接字符串添加到app.config文件。您将看到以下内容:

<connectionStrings>
        <clear />
        <add name="MSSQLContext" connectionString="...valid conn string..." />
        <add name="MySqlContext" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=mycontext;uid=root;password=********"/>
      </connectionStrings>

这是管理两个数据库中的数据的一种方法。它有自己的成本,例如:您必须维护两组实体(我假设两组中的数据相同)。

答案是肯定和否定。它支持多个数据库,但您不会有一个DbContext来通信您拥有的两种类型的数据库。您必须使用它们的连接字符串构建两个上下文(我建议将它们放在单独的项目中)您可以在您的业务逻辑中实例化这两个上下文。但这会产生问题。如何在此处同时添加mysql和sql server连接设置。@Sayusiandoth这就是为什么我写道您需要一个用于mysql的项目,另一个用于MS sql的项目。两个项目都有app.config,您可以在其中定义所需的connectionFactory。
  <connectionStrings>
    <clear />
    <add name="MSSQLContext" connectionString="...valid conn string..." />
  </connectionStrings>
    public class MyBusinessContextWithMSSQL : DbContext
    {
        public MyBusinessContextWithMSSQL() : base("name=MSSQLContext")
        {
            this.DisableLazyLoad();
        }

    // Add your entities and mappings here
    }
<connectionStrings>
        <clear />
        <add name="MSSQLContext" connectionString="...valid conn string..." />
        <add name="MySqlContext" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=mycontext;uid=root;password=********"/>
      </connectionStrings>
public class BusinessLogic{

    public void SomeMethod() {

        // doing stuff with data in MS SQL
        using(MyBusinessContextWithMSSQL msSqlContext = new MyBusinessContextWithMSSQL()) {
            // your business logic
        }

        // doing stuff with data in MySQL
        using(MyBusinessContextWithMySQL msSqlContext = new MyBusinessContextWithMySQL()) {
            // your business logic
        }    
    }    
}