5

For example, I have following DbContext classes.

public class AppDbContext : DbContext {
    ...
}
public class LogDbContext : DbContext {
    ...
}
public class FooDbContext : DbContext {
    ...
}

If a connection string named AppDbContext is on the App.Config and I want other DbContext classes to share the same connection string as AppDbContext, could I just pass the string "AppDbContext" as the parameter for the ctor of LogDbContext and FooDbContext. For example,

public class FooDbContext : DbContext {
    public FooDbContext : base("AppDbContext") { }
}

Does it have any side effects ?

Update 2013/1/9

After trying @ShinH2S's suggestion and somethings, I give up this way, and decide give different Dbcontext derived classes with different connectionStrings and database. I have try a test project and put it on GitHub. It will throw a runtime exception when the entityframework detects the database scheme is changed because the AppDbContext and FooDbContext have different schemas. If I assign a DropCreateDatabaseIfModelChanges strategy to both DbContext derived classes, one of them will be dropped because the models is different to another.

Update 2017/10

This is an old problem. In my memory, EF6 and above versions can have different migration history for multiple context in the same migration table. I prefer this answer at SO. I had not been coding with C# about 2 years.

1 Answer 1

5

IMHO, there is no side effects. But if it was me I will just create a base class that inherits from DbContext class BaseDbContextthen all the contexts (AppDbContext, LogDbContext and FooDbContext ) will derive from BaseDbContext.

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

8 Comments

If those XXXDbContext derive from BaseDbContext, how do I make them have the same connection string so they use the same database ? Do I configure a connect string named BaseDbContext and all of derived classes use BaseDbContext for connecting ?
First you create one conection string named BaseDbContext in your configuration file. in your BaseDbContext class you create a default constructor like that : public BaseDbContext() : base("BaseDbcontext") { }. That is all you have to do.
I need to try it. Those XXXDbContext are not in the same class library, and I failed to create tables as this way public FooDbContext() : base("AppDbContext").
What is the error ? I dont understand why you need to do public FooDbContext() : base("AppDbContext"). The constructor of your derived class must like that : public FooDbContext() : base() {}. There is no need to pass the name of the connection string beacause it is already in the base class.
The FooDbContext's tables are not created in the same database. And now I need to find out what happens. Today my work will focus on this problem.
|

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.