Entity Framework Code First: Configuration.cs seed or custom initializer

后端 未结 1 2075
夕颜
夕颜 2021-02-14 14:11

I am working with the the Code First style of the Entity Framework for my first time. I want to set up some default data. The first approach I came across involved creating a cu

相关标签:
1条回答
  • 2021-02-14 14:19

    The Configuration.cs Seed method will run every time your model changes to make sure that some specific data stays in your DB, or to even possibly to reset that data to a specified default setting.

    The Custom Initializer's seed method, on the other hand, can be setup to run every single time the application loads, like in this code, which is currently in the Global.asax file of my MVC page:

    Database.SetInitializer(new MyCustomInitializer<MyDbContext, Configuration>());
    var db = new MyDbContext();
    db.Database.Initialize(true);
    

    The practical difference really comes into play after you deploy your application. The Custom Initializer will make sure that no user can destroy some data that's absolutely required in your program.

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