问题描述
在我的解决方案中,我有 2 个使用 Entity Framework 6 的项目.每个项目都指向不同的数据库,都使用相同的数据提供 - 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 类型相同的程序集中,使用 DbContext 类型上的 DbConfigurationTypeAttribute 来指定 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(启动项目的)中创建一个实体框架部分,但出现以下错误:
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?
推荐答案
拥有多少 DbContext 并不重要(在实体框架 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.
那么你准备好了.
具有两个 connectionString 和 Ef 6.01 & 的 appConfig 示例Sql Compact 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.
祝你好运
这篇关于配置多数据库Entity Framework 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!