且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

code-首先加入迁移将保持在同一列

更新时间:2022-11-01 22:44:18

事实证明,还有就是你的了.Designer.cs 类模型的一个隐藏的快照与迁移有关。 (这是 IMigrationMetadata.Target ,而不是人工可读的。)的一系列问题引起的步骤是:

As it turns out, there is a hidden snapshot of your model in the .designer.cs class associated with your migration. (This is IMigrationMetadata.Target, and is not human-readable.) The series of steps that caused the problem were:

  1. 创建更改模型。
  2. 运行添加迁移以创建为改变迁移。 (这将创建隐藏的 IMigrationMetadata.Target 值。)
  3. 注意到,有在迁移建模的方式错误,直接更改模型类和移民类。 (该 IMigrationMetadata.Target 值现在出不同步。)
  4. 运行更新 - 数据库以应用更改。
  1. Create changes to the model.
  2. Run Add-Migration to create the migration for the changes. (This creates the hidden IMigrationMetadata.Target value.)
  3. Noticing that there are errors in the way the migration was modelled, change the model class and migration class directly. (The IMigrationMetadata.Target values is now out-of-sync.)
  4. Run Update-Database to apply the changes.

要摆脱困境,用创建一个虚拟的迁移添加迁移虚拟,然后删除一切从向上()向下()的方法。

To get out of the mess, create a dummy migration using Add-Migration Dummy, then delete everything from the Up() and Down() methods.

注意添加迁移并就此发出警告;当您运行添加迁移,它显示:

Note that Add-Migration does warn you about this; when you run Add-Migration, it displays:

在设计code为这种迁移文件中包含的快照您   目前的code首先模型。该快照用于计算   当你脚手架更改模型中的下一个迁移。如果你   让你想在这个包含到模型中的其他变化   迁移,那么你可以通过运行重新脚手架它添加迁移   假了。

The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration Dummy' again.

警告只是没有任何意义,直到我想通了什么问题,看到隐藏的 IMigrationMetadata.Target 值。

The warning just didn't make any sense until I figured out what the problem was, and saw the hidden IMigrationMetadata.Target value.

底线:不要手动保持你的模型和迁移向上()方法同步;你必须重新执行添加迁移正确设置隐藏的价值。

Bottom line: don't manually keep your model and migration Up() methods in sync; you have to re-run Add-Migration to set the hidden value correctly.