如何在同一个 .cs 文件上重新创建初始迁移 [英] How to re-create initial migration on the same .cs file

查看:17
本文介绍了如何在同一个 .cs 文件上重新创建初始迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As common in EF Code First I've generated an "Initial Create" migration file where is located an outdated model of my db (I'm developing the app so the model is still changing). Now I have a "new model" defined in my code and instead of creating a new migration for that I just want to update the already existing file, because it still is the initial create migration.

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

I've tried using this without any luck

更新数据库-targetmigration $initialcreate

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

I also tried this one, but it's always creating a new .cs file

添加-迁移初始创建

感谢您的帮助

推荐答案

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

Here you can see how I solved this.

请谨慎使用这种方法,因为在我的场景中,我仍处于开发阶段,因此我不需要保留数据库或数据.

如果要覆盖当前的InitialCreate"文件并重新生成数据库,请按以下步骤操作:

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

1) Run in Package Manager Console:

添加-迁移 InitialCreate -force

Add-Migration InitialCreate -force

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

2) Delete the physical database (use SSMS, T-SQL or your prefered)

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

3) Run in Package Manager Console:

更新-数据库-TargetMigration:0 |更新数据库-force |更新数据库-force

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

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

1) 删除当前InitialCreate".cs 文件

1) Delete current "InitialCreate" .cs file

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

3) Run in Package Manager Console:

添加-迁移初始创建

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

4) Delete the physical database (use SSMS, T-SQL or your prefered)

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

4) Run in Package Manager Console:

更新-数据库-TargetMigration:0 |更新数据库-force |更新数据库-force

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

这篇关于如何在同一个 .cs 文件上重新创建初始迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆