为什么代码优先迁移无法正常工作?
Why Code-First Migration not worked Correctly?
我使用代码优先迁移,但在 运行 应用程序时出现错误。
error:Cannot 查找对象 "dbo.Products" 因为它不存在或您没有权限。
我已经在 sql server.I 上手动删除了 Products table 预计当 Re-运行 Project All table recreate if not exist in sql server .
此外,我为配置迁移写了 2 行。
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
有帮助吗?
如果您正在使用迁移,则不应"manually" 更改数据库。所以现在 EF 正在生成代码来为您删除 table,但它不存在。解决方案是注释掉 Up() 方法中删除 table 和更新数据库的代码。参见 https://msdn.microsoft.com/en-US/data/dn481501
如果要重新创建数据库,则需要切换到初始化程序,例如 DropCreateDatabaseIfModelChanges。参见 http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx。
我使用代码优先迁移,但在 运行 应用程序时出现错误。 error:Cannot 查找对象 "dbo.Products" 因为它不存在或您没有权限。 我已经在 sql server.I 上手动删除了 Products table 预计当 Re-运行 Project All table recreate if not exist in sql server .
此外,我为配置迁移写了 2 行。
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
有帮助吗?
如果您正在使用迁移,则不应"manually" 更改数据库。所以现在 EF 正在生成代码来为您删除 table,但它不存在。解决方案是注释掉 Up() 方法中删除 table 和更新数据库的代码。参见 https://msdn.microsoft.com/en-US/data/dn481501
如果要重新创建数据库,则需要切换到初始化程序,例如 DropCreateDatabaseIfModelChanges。参见 http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx。