简体   繁体   English

切换分支时如何解决Update-Database问题?

[英]How to solve Update-Database issue when switching branch?

I have one entity which has a column as int in Branch A . 我有一个实体,在Branch A中有一个列为int Now I switch to a different Branch B . 现在,我切换到另一个B分支。 There I create a migration and change column to varchar and run Update-Database . 在这里,我创建一个迁移并将列更改为varchar并运行Update-Database Now If I switch to Branch A again. 现在,如果我再次切换到分支A。 If I see in the code the column datatype is int , but in the database, its datatype is varchar . 如果我在代码中看到列数据类型为int ,但是在数据库中,其数据类型为varchar But I want its datatype to be int . 但是我希望它的数据类型为int I can not even remove this migration from Branch A because it was created in Branch B .I can see only one way to solve this issue is to delete the database and run Update-Database , But I will lose all data by doing this. 我什至不能从分支A中删除此迁移,因为它是在分支B中创建的。我只能看到一种解决此问题的方法是删除数据库并运行Update-Database ,但是这样做会丢失所有数据。 Is there any better way to solve this issue. 有没有更好的方法来解决此问题。

I'm using entity framework core 2.0.1. 我正在使用实体框架核心2.0.1。

I can think of two options: 我可以想到两种选择:

  1. Use a different database for each branch (probably the best idea, IMHO) 为每个分支使用不同的数据库(最好的主意,恕我直言)
  2. Before switching back to branch A , un-apply the migration from the database with Update-Database name-of-previous-migration 切换回分支A之前,请先取消使用Update-Database name-of-previous-migration从数据库进行Update-Database name-of-previous-migration

For example, let's assume you have the following migrations: 例如,假设您进行了以下迁移:

  • 20180214094058_Initial.cs
  • 20180216100541_FirstMigration.cs
  • 20180218185111_MigrationBranchB.cs

You can un-apply MigrationBranchB with Update-Database FirstMigration (this will call the Down method of MigrationBranchB ). 您可以通过Update-Database FirstMigration取消应用MigrationBranchB (这将调用MigrationBranchBDown方法)。

Relevant documentation : 相关文件

Reverting a migration 还原迁移

If you already applied a migration (or several migrations) to the database but need to revert it, you can use the same command to apply migrations, but specify the name of the migration you want to roll back to. 如果您已经对数据库应用了迁移(或多个迁移),但是需要还原数据库,则可以使用相同的命令来应用迁移,但是要指定要回滚的迁移的名称。

Update-Database LastGoodMigration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM