简体   繁体   中英

Entity Framework 6.0 with empty DbContext-Constructor

I have one ASP-WebForms-Application for 3 Companies. Every Company has its own Database and own EDMX-Model. 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.

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.

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!

Is it possible to intialize a Null(Fake,Pseudo)-DbContext or something and then change it to the Model-Context?

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. But although giving the Context a specific Model inside the method, I am returning a DbContext an can not use Object-Attributes after that.

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?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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