gpt4 book ai didi

entity-framework - 如何在同一个 .cs 文件上重新创建初始迁移

转载 作者:行者123 更新时间:2023-12-03 23:39:14 24 4
gpt4 key购买 nike

与 EF Code First 中常见的一样,我生成了一个“初始创建”迁移文件,该文件位于我的数据库的过时模型(我正在开发应用程序,因此模型仍在更改)。现在我在我的代码中定义了一个“新模型”,而不是为此创建一个新的迁移,我只想更新已经存在的文件,因为它仍然是初始创建迁移。

我试过用这个没有任何运气

Update-database -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 文件

Add-Migration InitialCreate



我会感谢你的帮助

最佳答案

在这里你可以看到我是如何解决这个问题的。

小心使用这种方法,因为在我的场景中,我仍处于开发阶段,因此我不需要保留数据库和数据。

如果要覆盖当前的“InitialCreate”文件并重新生成数据库,请执行以下步骤:

1) 在包管理器控制台中运行:

Add-Migration InitialCreate -force



2) 删除物理数据库(使用 SSMS、T-SQL 或您喜欢的)

3) 在包管理器控制台中运行:

Update-Database -TargetMigration:0 | update-database -force | update-database -force



如果您不关心当前的“InitialCreate”文件本身,想新建一个并重新生成数据库,请按照以下步骤操作:

1) 删除当前的“InitialCreate”.cs 文件

3) 在包管理器控制台中运行:

Add-Migration InitialCreate



4) 删除物理数据库(使用 SSMS、T-SQL 或您喜欢的)

4) 在包管理器控制台中运行:

Update-Database -TargetMigration:0 | update-database -force | update-database -force

关于entity-framework - 如何在同一个 .cs 文件上重新创建初始迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37693549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com