简体   繁体   中英

EF run Seed method without model changes

I have a webAPI hosted in Azure as my back-end. I am using Code-First to create my entities. I can pre-feed my DB fine via the seed method, but if I am done with my model changes and then remember that I need to add something to the seed, there is no way to run the seed method. Multiple posts suggest the simple run update-database. I do see: "Running Seed method." and then publish to azure, my table is still empty. And I don't want to change the models just to trigger the seed, because that is just not clean solution.

I have already tried using each of the commented lines but no avail

    public MyContext() : base("name=MyContext")
    {
        //Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, MyProject.Migrations.Configuration>("DefaultConnection"));
        //Database.SetInitializer<MyContext>(new DropCreateDatabaseAlways<MyContext>());
    }

is there something I should be doing at the azure side or it just plainly doesn't work unless I change one my models!?

Update1:

I already the migrations folder and follow the commands to enable it and update the db and when I have a model change, the db updates just fine. That works, no issue there. The only issue if I want to add things to the seed method and I want to execute it without having to change my model.

If you are using code first then you can manage and handle all your migrations easily.

you need to type "enable-migrations" using package manger console.After this you will have a folder named Migrations.

Check this link for additional information.

Let know if you have any issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM