update-database -force命令未更新实体框架代码中的基础-first [英] update-database -force command is not updating the base in entity framework code -first
问题描述
这个问题似乎重复了.但是这是我使用该命令所面临的问题.
May be this question seems duplicate. But here are the issue that I am facing with the command.
如果我运行此命令,
update-database -force
我遇到的第一个错误.
数据库中已经有一个名为tblAbc的对象
There is already an object named tblAbc in the database
然后我用Google搜索,每次得到一个stackoverflow链接以建议运行 添加迁移初始-IgnoreChanges
Then I googled and everytime I get a stackoverflow link for suggesting to run Add-Migration Initial -IgnoreChanges
如果我先运行此命令,然后再运行更新命令,则没有错误-正在运行种子方法
If I run this command and then run the update command, No error - Running seed method
但是它不会将数据库与新更新同步
But it doesn't sync the database with the new updates
然后,我尝试了几次,但问题相同,因此我寻找了一些替代解决方案&我知道了.
Then I tried for number of times but same issue, hence I looked for some alternate solution & I got.
使用update-database -Script,但是如果我运行此命令并得到以下错误.
to use update-database -Script but if I run this command and get the following error.
用户取消了保存"对话框
User canceld Save dialog box
然后我尝试通过打开SQL Managment Studio和
Then I tried to fix this by opening SQL Managment Studio and
工具>>选项>>设计者>>取消选中了防止需要重新创建表的防止保存更改
Tools>>Options>>designers>> unchecked the Prevent saving changes that required table recreation
但是在这之后同样是错误消息.
But after this also same error message.
然后我尝试在上下文类中添加以下配置
Then I tried to add the following coniguration in my context class
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion <context, Configuration>());
它运行成功并且运行了Seed方法,但是它不同步最新更新.
It ran with success and Running Seed method but it doesn't sync the latest updates.
推荐答案
使用现有数据库
根据您的上下文状态,您需要执行以下操作之一...
Depending on the state of your context you need to the one of the following...
如果您的数据库上下文仅包含您现有的表并且没有任何自定义更改,请执行以下操作
Add-Migration Initial -IgnoreChanges
这将创建一个空白的迁移脚本
This will create a blank migration script
update-database
这会将数据库更新为该迁移,但不会应用任何更改.现在,您可以将更改添加到数据库上下文中.完成后,请执行以下操作
This will update the database to this migration but no changes will be applied. You can now add your changes to the database context. After you have finished do the following
Add-Migration Custom
这将生成一个包含您所做的更改的迁移脚本.然后再次更新您的数据库.
This will generate a migration script with your changes in it. Then update your database again.
update-database
如果您的数据库上下文包含了现有表,并且您也进行了自定义更改
Add-migration Initial
这将生成一个迁移脚本.浏览迁移脚本,并删除UP和DOWN方法中对现有表的所有引用.您将得到一个仅包含自定义逻辑的脚本.
This generates a migration script. Go through the migration script and remove any references to existing tables in both the UP and DOWN methods. You will be left with a script which only includes your custom logic.
update-database
希望这会有所帮助!
这篇关于update-database -force命令未更新实体框架代码中的基础-first的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!