When is the Seed method called in a EF code first migrations scenario?

我只是一个虾纸丫 提交于 2019-11-29 16:37:06

问题


I'm new in a project and there is this class for the seed data:

 internal sealed class Configuration : DbMigrationsConfiguration<DAL.Context>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

And this code to start the seed:

protected override void Seed(Context context)
    {
        try
        {

My question is: when is the Seed method called? Only when a user does update-database and the user doesn't have the database (basicly a new user), or also when the user with an existing database calls an update-database?


回答1:


Seed method is used to initialized the database tables with some starting data. Whenever you run the migration and update the database it will run the seed method. Mostly it is used during the testing phase where you often need to recreate the database and populate database tables with sample data. Please go through this link http://blog.oneunicorn.com/2013/05/28/database-initializer-and-migrations-seed-methods/ for more explaination on code first.




回答2:


When it comes to the migrations Seed() method, coming from DbMigrationsConfiguration class, it's called every time when the Update-Database command is executed. Also when user calls it having existing database.

There is yet another Seed() method - it is a database initializer. It's invoked when database is creating and doesn't handle existing data (while seed from DbMigrationsConfiguration handles them, checking if specified entities exist).

Good to look up to One Unicorn blog for more information.



来源:https://stackoverflow.com/questions/24142107/when-is-the-seed-method-called-in-a-ef-code-first-migrations-scenario

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!