EF迁移和时间戳列无法更新/运行

时间:2015-02-26 06:52:55

标签: c# .net entity-framework entity-framework-6

我在EF6中有一个对象,我忘记继承我的auditableEntity类。这个类有这样的配置

public abstract class AuditableEntityConfig<TEntity> : BaseEntityConfig<TEntity> where TEntity : AuditableEntity
{
    public AuditableEntityConfig()
        : base()
    {

        this.Property(e => e.RowVersion)
            .IsRowVersion();

    }
}

现在我已经更新了我的实体以继承这个类,现在运行我的代码时,我总是收到错误说

Cannot alter column 'RowVersion' to be data type timestamp.

无论如何,我可以阻止EF尝试将此列设置为时间戳,也许我自己放弃并重新创建表格?

2 个答案:

答案 0 :(得分:2)

步骤1.编辑表定义mannualy:

   [RowVersion]   TIMESTAMP NOT NULL

步骤2.转到Migrations目录中的NameOfMigrationAdded.cs并在Up()方法中注释该行:

   AlterColumn("dbo.YOUR_TABLE_NAME_HERE", "RowVersion", c => c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"));

步骤3.在PM控制台中运行“Update-Database”命令

现在可行;)

步骤4.返回上一次迁移并通过添加:

编辑Up()方法
 Version = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"),

在CreateTable(...)或AddColumn(...)

例如:

  AddColumn("dbo.Rows", "RowVersion", c => c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"));

答案 1 :(得分:2)

  

替换一行AlterColumn,其中包含两行DropColumnAddColumn的Up()方法。

        public override void Up()
        {
            DropColumn("dbo.Cities", "RowVersion", null);
            AddColumn("dbo.Cities", "RowVersion", c => c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"));
        }

        public override void Down()
        {
            AlterColumn("dbo.Cities", "RowVersion", c => c.Binary());
        }