How to recreate database in EF if my model changes?

前端 未结 3 1443
慢半拍i
慢半拍i 2020-12-10 18:55

I created a DbContext like so :

   public class myDB : DbContext
   {
     public DbSet Parties { get; set; }
     public DbSet B         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 19:32

    I believe you're looking for:

    Database.SetInitializer(new DropCreateDatabaseIfModelChanges());
    

    As of EF 4.1 and above.

    Note that this assumes you have permission to even drop your database. I do this locally for ease of development but disable it in staging/production (I do a manual Schema Compare and push my changes).

    By the way, for testing, if you need to force recreate the database, you can do:

    using (var context = new ClubmansGuideDB()) {
        context.Database.Initialize(force: true);
    }
    

    (using if you don't already have a reference to your DB)

提交回复
热议问题