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.

AechoLiu
  • 17,522
  • 9
  • 100
  • 118

1 Answers1

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.

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
  • 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 ? – AechoLiu Jan 08 '13 at 09:14
  • 1
    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. – CodeNotFound Jan 08 '13 at 09:23
  • 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")`. – AechoLiu Jan 08 '13 at 10:31
  • 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. – CodeNotFound Jan 08 '13 at 10:59
  • 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. – AechoLiu Jan 09 '13 at 01:57
  • How did you go @Toro? I having the same problem. I ended up combining the context into one class as there wasn't too much a need to. With @SlimH2S solution I tried though it also didn't work. Should only BaseDbContext have an Initializer class or all dbConexts have independent initizalizers? – tourdownunder Jan 29 '13 at 02:43
  • I put a sample project at GitHub, you can find it on my `Update` post. I think that those different contexts will share the same `dbo._MigrationHistory` , and one of them will treat the db scheme has been changed due to the this table. I give up sharing the same connectionstring, and assign new one string to each context. – AechoLiu Jan 29 '13 at 04:34
  • I think you must watch Julie Lerman video (http://pluralsight.com/training/courses/TableOfContents?courseName=efarchitecture&highlight=julie-lerman_efarchitecture-m2-boundedcontext*6#efarchitecture-m2-boundedcontext) about Bounded Contexts and how to make multiple context to point to the same database. It will help you a lot ;) – CodeNotFound Jan 29 '13 at 12:51