简体   繁体   English

具有空 DbContext-Constructor 的实体框架 6.0

[英]Entity Framework 6.0 with empty DbContext-Constructor

I have one ASP-WebForms-Application for 3 Companies.我有一个适用于 3 家公司的 ASP-WebForms-Application。 Every Company has its own Database and own EDMX-Model.每家公司都有自己的数据库和自己的 EDMX 模型。 The Structure in the Databases is the same.数据库中的结构是相同的。 On Parameters I am checking which Company it is and want to have one DbContext for all the Models.在参数上,我正在检查它是哪家公司,并希望为所有模型拥有一个 DbContext。

I am very new in Entity Framework and don't know how to make one Context for a few Models.我是实体框架的新手,不知道如何为几个模型制作一个上下文。

I have tried to make a class that gives me a DbContext, in which I want to make the DBContext one of the Models.我试图制作一个 class 给我一个 DbContext,我想在其中使 DBContext 成为模型之一。

I am trying:我在尝试:

public static DbContext holeDbContextWsvWsbSvs()
    {
        DbContext context = new DbContext();
        string verbandKürzel = config.holeVerbandKürzel();
        if (verbandKürzel == "wsv")
        {
            context = new wsvEntities();
        }
        else if (verbandKürzel == "wsb")
        {
            context = new wsbEntities();
        }
        else if (verbandKürzel == "svs")
        {
            context = new svsEntities();
        }
        return context;
    }

But in Entity Framework 6.0 it seems as an emtpy Constructor is not possible!但在 Entity Framework 6.0 中,似乎不可能使用 emtpy 构造函数!

Is it possible to intialize a Null(Fake,Pseudo)-DbContext or something and then change it to the Model-Context?是否可以初始化 Null(Fake,Pseudo)-DbContext 或其他东西,然后将其更改为模型上下文?

Can anyone give me an impulse please, how can i realize my plans?谁能给我一个冲动,我怎样才能实现我的计划?

EDIT: Okay, changing the Context to the Model inside the Method achieved by giving the Constructor any string.编辑:好的,通过为构造函数提供任何字符串来实现方法内的 Model 的上下文更改。 But although giving the Context a specific Model inside the method, I am returning a DbContext an can not use Object-Attributes after that.但是,虽然在方法内部给 Context 一个特定的 Model,我返回一个 DbContext 之后不能使用 Object-Attributes。

Any Suggestions?有什么建议么? Or do really have to make at every position i need to work with the DataModel IF-ELSEs and do the same logic for each Model?或者真的必须在每个 position 我需要使用 DataModel IF-ELSE 并为每个 Model 执行相同的逻辑?

If the structure of the DbContexts are identical and you just want to point at one of three databases, you don't need to declare 3 separate DbContexts, just one, then use the Constructor argument to nominate the appropriate connection string.如果 DbContext 的结构相同并且您只想指向三个数据库之一,则无需声明 3 个单独的 DbContext,只需一个,然后使用 Constructor 参数指定适当的连接字符串。

public class AppDbContext : DbContext
{
    // Define DbSets etc.

    public AppDbContext(string connection) 
        : base (connection)
    { }
}

Then in your initialization code:然后在您的初始化代码中:

public AppDbContext holeDbContextWsvWsbSvs()
{
    string verbandKürzel = config.holeVerbandKürzel();
    switch (verbandKürzel)
    {
        case "wsv":
            return new AppDbContext("wsvEntities");
        case "wsb":
            return new AppDbContext("wsbEntities");
        case "svs":
            return new AppDbContext("svsEntities");
        default:
            throw new InvalidOperationException("The configured client is not supported.");
    }
}

Assuming you have connections strings called "wsvEntities", "wsbEntities", and "svsEntities" respectively.假设您有分别称为“wsvEntities”、“wsbEntities”和“svsEntities”的连接字符串。

If the DbContexts are not identical in structure then honestly you likely will have much bigger problems that won't be solved by exposing them via the base DbContext.如果 DbContexts 在结构上不相同,那么老实说,您可能会遇到更大的问题,这些问题无法通过基础 DbContext 公开它们来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM