【发布时间】:2016-10-08 04:38:28
【问题描述】:
在 EF Code First 中很常见,我生成了一个“初始创建”迁移文件,该文件位于我的数据库的过时模型(我正在开发应用程序,因此模型仍在更改)。现在我在我的代码中定义了一个“新模型”,而不是创建一个新的迁移,我只想更新已经存在的文件,因为它仍然是初始创建迁移。
我试过用这个没有任何运气
更新数据库-targetmigration $initialcreate
返回
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
You can use the Add-Migration command to write the pending model changes to a code-based migration.
我也试过这个,但它总是创建一个新的 .cs 文件
添加-迁移初始创建
感谢您的帮助
【问题讨论】:
-
据我了解,您不想创建新的迁移?如果那时您可以尝试删除整个迁移文件夹并再次执行
Enable-Migration。您将看到新的InitialCreate文件将包含您的模型。但是,这似乎不合适,因为代码优先迁移旨在列出包含您在模型中添加的内容的不同迁移时期(无论是类还是财产)。您可以选择回滚到之前的迁移(您已经使用-targetmigration进行过迁移),因此非常灵活。 -
另外,如果上述方法不起作用,您可以尝试
Update-Database -TargetMigration:0。 -
@ShawnYan 是的,我不想创建新的迁移,因为我的模型仍在更改,所以还没有“迁移”。
-
好的,我明白了,你能回滚到初始创建吗?
-
@ShawnYan 是的,我可以,我的想法是在数据库设计过程结束之前只保留一个“初始迁移”文件
标签: entity-framework entity-framework-6