code-首先加入迁移将保持在同一列 [英] Code-First Add-Migration keeps adding the same column

查看:196
本文介绍了code-首先加入迁移将保持在同一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型包含,除其他事项外,添加的初始创建和迁移后的模型类的两个属性。

My model contains, among other things, two properties that were added to the model class after its initial creation and migration.

public sealed class SomeModel
{
    ... other properties, which are fine.        
    public int PropertyOne { get; set; }
    public int PropertyTwo { get; set; }
}

我最近的移民包括:

My most recent migration contains:

public override void Up()
{
    ... other table being created.
    AddColumn("dbo.SomeModel", "PropertyOne", c => c.Int(nullable: false));
    AddColumn("dbo.SomeModel", "PropertyTwo", c => c.Int(nullable: false));
}

目标数据库中包含 PropertyOne PropertyTwo 列,而 __ MigrationHistory 表中包含的条目为创建该表并添加列在迁移的迁移。

The target database contains the PropertyOne and PropertyTwo columns, and the __MigrationHistory table contains entries for both the migration that created the table and the migration that added the columns.

当我运行添加迁移来得到一些其他的变化,它也同样包括这两个属性:

When I run Add-Migration to get some other changes, it also includes those two properties again:

public override void Up()
{
    ... other changes.
    AddColumn("dbo.SomeModel", "PropertyOne", c => c.Int(nullable: false));
    AddColumn("dbo.SomeModel", "PropertyTwo", c => c.Int(nullable: false));
}

什么引起的?我也注意到,如果我恢复了我所有的模型的变化,并尝试了更新 - 数据库(应该什么都不做),我得到的错误:

What could be causing this? I also notice that if I revert all of my model changes and try an Update-Database (which should do nothing), I get the error:

无法更新数据库以匹配当前的模型,因为有   挂起的更改,并自动迁移被禁用。无论是写   挂起模式更改为code为基础的迁移或启用自动   迁移。设置DbMigrationsConfiguration.AutomaticMigrationsEnabled到   真正启用自动迁移。

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.

什么引起的?

推荐答案

事实证明,还有就是你的了.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.

这篇关于code-首先加入迁移将保持在同一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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