后续 EF 迁移在数据库更新时失败:表已存在
我能够使用 Add-Migration InitialMigration
成功创建迁移,并根据 Microsoft doco。
对我的模型进行更改后,我添加了第二个迁移 Add-Migration UpdatedModel
,这有效。但是,这不是累积更改 - 它就像初始迁移一样,Up
方法包含 CreateTable
语句。
因此,update-database
命令在第二次及后续尝试中失败,并抱怨表已存在。
是我对 EF 的期望太高还是这看起来像是一个问题?看来迁移的重点是管理累积更新。
明显的解决方法是删除数据库和/或删除所有迁移文件,以便后续迁移始终是初始迁移。当我开始将种子数据之外的一些数据放入测试数据库时,这会变得混乱。
I am able to successfully create a migration with Add-Migration InitialMigration
, and apply it with update-database
, per the Microsoft doco.
Making changes to my model, I add a second migration Add-Migration UpdatedModel
, and this works. However, it is not a cumulative change - it is just like the initial migration, with the Up
method containing CreateTable
statements.
As a result, the update-database
command fails on that second and subsequent attempts, complaining that the tables already exist.
Am I expecting too much from EF or does this seem like an issue? It seems that the point of the migrations is to manage cumulative updates.
The obvious workaround is to drop the database, and/or delete all migration files, so that subsequent migrations are always the initial one. This will get messy as I start to get some data beyond seed data into the test database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
Migrations/AssetContextModelSnapshot.cs
文件是在初始迁移时生成的,但它并未包含在项目中。因此,该项目没有可用的迁移历史记录,并且每次迁移都是新的。右键->包含在项目中+全部保存,问题就解决了!The problem was that the
Migrations/AssetContextModelSnapshot.cs
file got generated with the initial migration, however, it was not included in the project. Therefore, the project did not have the history of migrations available to it, and each migration was new. Right-click -> Include in project + save all, and problem solved!