在小組環境中移轉Migrations in Team Environments

在小組環境中使用遷移時,請特別注意模型快照集檔案。When working with Migrations in team environments, pay extra attention to the model snapshot file. 這個檔案可以告訴您,您的組員遷移是否與您的遷移完全相同,或者,如果您需要在共用之前重新建立您的遷移,以解決衝突。This file can tell you if your teammate's migration merges cleanly with yours or if you need to resolve a conflict by re-creating your migration before sharing it.

合併Merging

當您合併小組成員時,您可能會在模型快照集檔案中發生衝突。When you merge migrations from your teammates, you may get conflicts in your model snapshot file. 如果這兩個變更都不相關,則合併是很簡單的,而且這兩個遷移可以並存。If both changes are unrelated, the merge is trivial and the two migrations can coexist. 例如,您可能會在如下所示的 customer 實體類型設定中出現合併衝突:For example, you may get a merge conflict in the customer entity type configuration that looks like this:

<<<<<<< Mine
b.Property<bool>("Deactivated");
=======
b.Property<int>("LoyaltyPoints");
>>>>>>> Theirs

因為這兩個屬性都必須存在於最終模型中,所以請加入這兩個屬性來完成合併。Since both of these properties need to exist in the final model, complete the merge by adding both properties. 在許多情況下,您的版本控制系統可能會自動為您合併這類變更。In many cases, your version control system may automatically merge such changes for you.

b.Property<bool>("Deactivated");
b.Property<int>("LoyaltyPoints");

在這些情況下,您的遷移和組員的遷移彼此獨立。In these cases, your migration and your teammate's migration are independent of each other. 由於您可以先套用其中一種,因此在與小組共用之前,您不需要對您的遷移進行任何額外的變更。Since either of them could be applied first, you don't need to make any additional changes to your migration before sharing it with your team.

解決衝突Resolving conflicts

當您合併模型快照模型時,有時會遇到真正的衝突。Sometimes you encounter a true conflict when merging the model snapshot model. 例如,您和您的組員可能會將相同的屬性重新命名。For example, you and your teammate may each have renamed the same property.

<<<<<<< Mine
b.Property<string>("Username");
=======
b.Property<string>("Alias");
>>>>>>> Theirs

如果您遇到這類衝突,請重新建立您的遷移來解決此問題。If you encounter this kind of conflict, resolve it by re-creating your migration. 請遵循下列步驟:Follow these steps:

  1. 在合併之前中止合併並回復至您的工作目錄Abort the merge and rollback to your working directory before the merge
  2. 移除您的遷移 (但保持您的模型變更) Remove your migration (but keep your model changes)
  3. 將您的組員變更合併到您的工作目錄Merge your teammate's changes into your working directory
  4. 重新新增您的遷移Re-add your migration

這麼做之後,就可以依正確順序套用這兩個遷移。After doing this, the two migrations can be applied in the correct order. 首先會套用其遷移,並將資料行重新命名為 別名,之後您的遷移就會將它重新命名為使用者 名稱Their migration is applied first, renaming the column to Alias, thereafter your migration renames it to Username.

您可以安全地與小組的其他人共用您的遷移。Your migration can safely be shared with the rest of the team.