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

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

问题描述

在普通的EF代码首先我已经生成了一个初始创建迁移文件,其中位于我的数据库的过时模型(我开发的应用程序,所以模型仍在改变)。现在我在代码中定义了一个新模型,而不是为此创建一个新的迁移,我只想更新已经存在的文件,因为它仍然是最初的创建迁移。



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


更新数据库-targetmigration $ initialcreate


它返回

 无法更新数据库以匹配当前因为有待处理的更改和自动迁移被禁用。将挂起的模型更改写入基于代码的迁移或启用自动迁移。将DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移。 
您可以使用Add-Migration命令将挂起的模型更改写入基于代码的迁移。

我也尝试过这个,但它总是创建一个新的.cs文件


添加迁移InitialCreate


我会欣赏你的帮助

解决方案

这里可以看到我如何解决这个问题。



>请注意这种方法,因为在我的情况下,我还处于开发阶段,所以我不需要保留数据库和数据。



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



1)在程序包管理器控制台:


添加迁移InitialCreate -force


2)删除物理数据库(使用SSMS,T-SQL或您的首选)



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


更新数据库 - 目标迁移:0 | update-database -force | update-database -force


如果您不关心当前的InitialCreate文件本身,想要创建一个新的一个并重新生成DB,请按照下列步骤操作:



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



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


添加迁移InitialCreate


< blockquote>

4)删除物理数据库(使用SSMS,T-SQL或您的首选)



4)运行包管理员控制台:


更新数据库 - 目标迁移:0 | update-database -force | update-database -force



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

Update-database -targetmigration $initialcreate

It returns

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.

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

Add-Migration InitialCreate

I'll appreaciate your help

解决方案

Here you can see how I solved this.

Be careful with this approach because in my scenario I'm still in development phase so I do not need to keep the database nor the data.

If you want to overwrite the current "InitialCreate" file and re-generate the DB, follow these steps:

1) Run in Package Manager Console:

Add-Migration InitialCreate -force

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

3) Run in Package Manager Console:

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

If you do not care about the current "InitialCreate" file itself, want to create a new one and re-generate the DB, follow these steps:

1) Delete current "InitialCreate" .cs file

3) Run in Package Manager Console:

Add-Migration InitialCreate

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

4) Run in Package Manager Console:

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

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

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