配置多个数据库实体框架6

 mimiku2z_818 发布于 2023-02-13 16:35

在我的解决方案中,我有两个使用Entity Framework 6的项目.每个项目指向不同的数据库,两者都使用相同的数据提供 - SQL Server.我的解决方案中的第三个项目需要使用这两个数据库.我的问题是如何配置这些上下文.我尝试在单独的程序集中创建配置类:

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

并在每个上下文类中引用此配置:

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

初始化我的第一个时,所有工作都正确,但是当第二个上下文初始化时(顺序无关紧要)我得到并且错误:

设置了'EfDbConfiguration'的实例,但是在与'B1Entities'上下文相同的程序集中未发现此类型.将DbConfiguration类型放在与DbContext类型相同的程序集中,在DbContext类型上使用DbConfigurationTypeAttribute指定DbConfiguration类型,或在配置文件中设置DbConfiguration类型.有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260883.*

我还试图在我的app.config(启动项目)中创建一个entityframework部分,但是出现了以下错误:

配置系统无法初始化

无法识别的配置部分entityFramework

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

2 个回答
  • 你拥有多少个DbContexts并不重要(在实体框架6中).只需将连接字符串放在appConfig或启动项目的webConfig中.

    然后你准备好了.

    具有两个connectionString与Ef 6.01和Sql Compact 4.0的appConfig示例

    <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的例子:

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

    您的上下文是否在分离的项目中并不重要,只有启动项目的配置很重要.

    如果您需要任何其他信息,请告诉我.

    祝好运

    2023-02-13 16:37 回答
  • 虽然这个问题已经过时了,但是我被google引导到了这里,但没有找到答案.也许我的回答会对某人有所帮助.

    连接字符串EntityFramework 6应该位于执行文件夹中的(alert!)内部配置文件中.例如OP在解决方案中有多个项目,因此连接字符串必须在配置文件中属于主执行项目.

    现在,如果要在代码中定义连接字符串,可以在配置文件中创建伪连接字符串,并为实体的实例提供新的连接字符串:

    DBEntities e = new DBEntities();
    e.Database.Connection.ConnectionString = "Data Source=MyServ;Initial Catalog=MyDB;Persist Security Info=True;User ID=sa;Password=***;Application Name=MyApp";
    

    2023-02-13 16:39 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有