0

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?

5
  • Where are you storing your connection-strings? Commented Apr 15, 2021 at 11:37
  • In the WebConfig Commented Apr 15, 2021 at 11:37
  • 1
    What's the point of 3 classes if The Structure in the Databases is the same ? Commented Apr 15, 2021 at 12:24
  • Don't know if understanding right, but then i still have to do every method in each of the class. My intention is to everything only once with the context. Is there no possibillity to get the model of the context by only working with the context? Commented Apr 15, 2021 at 12:41
  • what do you mean by "to get the model of the context"? You're not using EF terminology correctly. Commented Apr 15, 2021 at 15:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.