配置多个数据库实体框架6 [英] Configure multiple database Entity Framework 6

查看:202
本文介绍了配置多个数据库实体框架6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的解决方案,我有使用实体框架6.各指向不同的数据库,都使用相同的数据提供了2个项目 - SQL Server中。
在我的解决方案第三个项目需要使用这两个数据库。我的问题是如何配置这些上下文。我想在一个单独的程序来创建一个配置类:

In my solution I have 2 projects that use Entity Framework 6. Each points to a different database, both using the same data provide - SQL Server. A third project in my solution needs to use both databases. My problem is how to configure those context. I tried to create a configuration class in a separate assembly:

namespace OSAD_Base
{
    class EfDbConfiguration : DbConfiguration
    {
        public EfDbConfiguration()
        {
            SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);
        }
    }
}

和引用该配置在每个上下文类:

and referencing to this configuration in each context class:

namespace IntegrationDb
{
    [DbConfigurationType("OSAD_Base.EfDbConfiguration, OSAD_Base")]
    public partial class IntegrationEntities : DbContext
    {
        public IntegrationEntities(string connectionString)
            : base(connectionString)
        {
        }
    }
}

在初始化我第一次,所有的作品正确的,但是当第二上下文初始化(顺序并不重要)我得到和错误:

When initializing my first, all works correct, but when the second context initializes (Order does not matter) I get and error:

EfDbConfiguration的一个实例设置,但在同一个程序集没有发现这种类型作为B1Entities'上下文。要么把DbConfiguration输入同样的装配为一体的的DbContext类型,使用DbConfigurationTypeAttribute上的DbContext类型指定DbConfiguration类型,或者设置在配置文件中的DbConfiguration类型。请参见 http://go.microsoft.com/fwlink/?LinkId=260883 了解详情。*

An instance of 'EfDbConfiguration' was set but this type was not discovered in the same assembly as the 'B1Entities' 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.*

我也试图创造我的app.config一个的EntityFramework部分(启动项目),但得到了以下错误:

I also tried to create an entityframework section in my app.config (of the start up project) but got the following error:

配置系统初始化失败

无法识别的配置节的EntityFramework

Unrecognized configuration section entityFramework

我如何使用2个独立的EF项目在同一个解决方案?

How can I use 2 separate EF Projects in the same solution?

推荐答案

这不是重要的,你有多少DbContexts有(实体框架6)。
只要把连接字符串中的AppConfig或启动项目的webConfig。

It's not important that how many DbContexts you have(In entity framework 6). Just put connection strings in appConfig or webConfig of startup project.

然后,你准备好了。

有两个与EF 6.01和放大器的ConnectionString AppConfig的实例; SQL精简4.0

Example of appConfig with two connectionString with Ef 6.01 & Sql Compact 4.0

<configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="MainDb" connectionString="Data Source=|DataDirectory|\Db.sdf" providerName="System.Data.SqlServerCe.4.0" />
    <add name="AnotherDb" connectionString="Data Source=|DataDirectory|\AnotherDb.sdf" providerName="System.Data.SqlServerCe.4.0" />
  </connectionStrings>
  <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>

和例子DbContexts的:

And example of DbContexts:

public class AppDb : DbContext
{
    public AppDb()
        : base("MainDb")
    {

    }
}

public class AnotherDb : DbContext
{
    public AnotherDb()
        : base("AnotherDb")
    {

    }
}

这不是重要的是你的上下文是在分离项目与否,只有启动项目的配置是非常重要的。

It's not important that your contexts are in separated projects or not, only Config of startup project is important.

让我知道如果任何其他信息,你所需要的。

Let me know if any other information you need.

好运

这篇关于配置多个数据库实体框架6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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