How do I use Entity Framework in Code First Drop-Create mode?

后端 未结 2 864
栀梦
栀梦 2020-12-01 04:36

I\'m using Entity Framework v4. I have followed the instructions in the Nerd Dinner tutorial. I\'m currently in development mode (not released to any higher environments)

相关标签:
2条回答
  • 2020-12-01 04:51

    Use DropCreateDatabaseAlways initializer for your database. It will always recreate database during first usage of context in app domain:

    Database.SetInitializer(new DropCreateDatabaseAlways<YourContextName>());
    

    Actually if you want to seed your database, then create your own initializer, which will be inherited from DropCreateDatabaseAlways:

    public class MyInitializer : DropCreateDatabaseAlways<YourContextName>
    {
         protected override void Seed(MagnateContext context)
         {
             // seed database here
         }
    }
    

    And set it before first usage of context

    Database.SetInitializer(new MyInitializer());
    
    0 讨论(0)
  • 2020-12-01 04:55

    If the database already exists and you want to make changes to your model, you use DropCreateDatabaseIfModelChanges<YourContextName>

    0 讨论(0)
提交回复
热议问题