EF运行种子方法没有模型变化 [英] EF run Seed method without model changes
问题描述
我在Azure中托管了一个webAPI作为我的后端。我正在使用Code-First来创建我的实体。我可以通过种子方法预先给我的数据库,但是如果我完成了我的模型更改,然后记得我需要添加一些种子,没有办法运行种子方法。
多个帖子建议简单运行update-database。我确实看到:跑种子。然后发布到天蓝色,我的桌子还是空的。我不想改变模型只是为了触发种子,因为这只是不干净的解决方案。
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!?
我已经是迁移文件夹,启用它并更新数据库的命令,以及当我更改模型时,数据库更新很好。那工作,没有问题。唯一的问题,如果我想添加的东西到种子方法,我想执行它,而不必更改我的模型。
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.
您需要使用包管理器控制台键入enable-migrations。此后,您将有一个名为Migrations的文件夹。
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.
这篇关于EF运行种子方法没有模型变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!