Friday 15 May 2015

c# - Multiple DbContexts on one DB with Code First Migrations -


I have raised this problem as well as in addition, I did not want to loosen the __magentising table from the database .

I suggested to use this "super" reference, in which all DbSet, & gt; Context, but I got an error ("Model backing dBcintext has been changed") If you just kill __MargissionHystig table from the SQL server, it can be easily accessible, but as I said, I want to keep history .

See me a simple and easy solution, my answer below.

First of all, you should create the "super" reference migration configuration.

  MySuperContext: DbContext {// All DbSet & lt; & Gt; One of your different references must be mentioned here, you will only use this class for migration. Public MySuperContext (): Base ("YourConnectionString") {System.Data.Entity.Database.SetInitializer (New MigrateDatabaseOutoteStarson & lt; MySpore Contact, Migration Confirmation & gt; ()); }}   

Then just create the following category:

  Public class NoDatabaseInitializer & lt; T & gt; : IDBS Starter & lt; T & gt; Where T: DbContext {public zero initial database (T reference) {// Nothing, this means! Now, in every small context, add it to the constructor:  
  class MyUserContext: DbContext {public MyUserContext: base ("MyConnectionString" ) // can be a user reference, etc. {System.Data.Entity.Database.SetInitializer (new nodeadvertiseriilizer & lt; MyContext & gt; ()); }}   

Now you will not get this error further, plus, your migration-history will be, and you will use multiple references on one database.

No comments:

Post a Comment