简体   繁体   English

EF运行Seed方法,无需更改模型

[英]EF run Seed method without model changes

I have a webAPI hosted in Azure as my back-end. 我在Azure中托管了一个webAPI作为我的后端。 I am using Code-First to create my entities. 我正在使用Code-First来创建我的实体。 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. 然后发布到azure,我的桌子仍然是空的。 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: 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. 您需要使用包管理器控制台键入“enable-migrations”。之后您将拥有一个名为Migrations的文件夹。

Check this link for additional information. 请查看此链接以获取更多信息。

Let know if you have any issues. 如果您有任何问题,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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