实体框架-使用相同的DbContext和不同的连接字符串 [英] Entity Framework - using the same DbContext with different connection strings

查看:58
本文介绍了实体框架-使用相同的DbContext和不同的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在应用程序中使用相同的DbContext(我有两个结构相同的数据库)。我不太确定自己在做什么错,但是这是我当前的代码-希望它可以很明显地说明我要做什么。我正在使用EF数据库优先(底部的错误似乎并不建议)。

I'm currently trying to use the same DbContext (I have two databases, of identical structure) in my application. I'm not quite sure what I'm doing wrong, but here's my current code - hopefully it should be pretty obvious what I'm trying to do. I'm using EF Database First (which the error at the bottom seems not to suggest).

我的上下文工厂代码:

public class HOLContextFactory
    {
        public static HOLDbEntities Create()
        {
            return new HOLDbEntities(); // Works
        }

        public static HOLDbQuoteEntities CreateQuote()
        {
            return new HOLDbQuoteEntities(); // Gives error
        }
    }

public partial class HOLDbQuoteEntities : HOLDbEntities
    {
        public HOLDbQuoteEntities()
            : base("HOLDbQuoteEntities") // This should send "HOLDbQuoteEntities" as the base connection string?! 
// Also tried "name=HOLDbQuoteEntities"
            {
            }
        }

Web.config连接字符串:

Web.config connection strings:

<add name="HOLDbEntities" connectionString="metadata=res://*/HOLDbContext.csdl|res://*/HOLDbContext.ssdl|res://*/HOLDbContext.msl;provider=System.Data.SqlClient;provider connection string=<connstringdetails>" providerName="System.Data.EntityClient" />

<add name="HOLDbQuoteEntities" connectionString="metadata=res://*/HOLDbContext.csdl|res://*/HOLDbContext.ssdl|res://*/HOLDbContext.msl;provider=System.Data.SqlClient;provider connection string=<connstringdetails>" providerName="System.Data.EntityClient" /> // using diff database - same structure

使用 HOLDbQuoteEntities时出现错误:

Error I'm getting when using "HOLDbQuoteEntities" :


使用T4模板为Database First和Model
First开发生成的代码如果在Code First模式下使用可能无法正常工作。
要继续使用数据库优先或模型优先,请确保在
执行应用程序的配置文件中指定了实体
框架连接字符串。要使用这些类,这些类是从
数据库优先或模型优先生成的,使用代码优先,使用属性或DbModelBuilder API添加任何其他
配置,然后
删除引发此异常的代码* *

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception**


推荐答案

实体框架需要使用实际的实体对象:

Entity Framework needs to use the actual entities object:

public class HOLContextFactory
{
    public static HOLDbEntities Create()
    {
        // default connection string
        return new HOLDbEntities(); 
    }

    public static HOLDbEntities CreateQuote()
    {
        // specified connection string
        return new HOLDbEntities ("HOLDbQuoteEntities"); 
    }
}

public partial class HOLDbEntities
{
    public HOLDbEntities(string connectionString)
        : base(connectionString) 
        {
        }
    }
}

这篇关于实体框架-使用相同的DbContext和不同的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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