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

Gil*_*rdo 5 .net c# 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();

    }
}
Run Code Online (Sandbox Code Playgroud)

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

Cannot alter column 'RowVersion' to be data type timestamp.
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*eio 5

AlterColumn用 2 行DropColumnAddColumnUp() 方法替换一行。

        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());
        }
Run Code Online (Sandbox Code Playgroud)


Łuk*_*ski 3

步骤1.手动编辑表定义:

   [RowVersion]   TIMESTAMP NOT NULL
Run Code Online (Sandbox Code Playgroud)

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

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

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

现在可以了;)

步骤 4. 返回到之前的迁移并编辑 Up() 方法,添加:

 Version = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"),
Run Code Online (Sandbox Code Playgroud)

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

前任:

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