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 ?
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.
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.
IMHO, there is no side effects. But if it was me I will just create a base class that inherits from DbContext class BaseDbContext
then all the contexts (AppDbContext
, LogDbContext
and FooDbContext
) will derive from BaseDbContext
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With