You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级EF Core 9.0.1时,时态表与乐观并发引发Version列修改失败

问题描述

自EF Core 7.0.0起便使用时态表,且更早实现了乐观并发,二者此前协同正常。但升级至.NET 9及Microsoft.EntityFrameworkCore 9.0.1后,新数据库无法执行迁移(已完成迁移的数据库可正常运行)。

迁移执行情况

  1. 首次迁移可正常执行:
_ = migrationBuilder.AddColumn<byte[]>(
    name: "Version",
    table: "Vulnerability",
    type: "rowversion",
    rowVersion: true,
    nullable: true);
  1. 执行时态表迁移时触发异常:
_ = migrationBuilder.AlterColumn<byte[]>(
    name: "Version",
    table: "Vulnerability",
    type: "rowversion",
    rowVersion: true,
    nullable: true,
    oldClrType: typeof(byte[]),
    oldType: "rowversion",
    oldRowVersion: true,
    oldNullable: true)
    .Annotation("SqlServer:IsTemporal", true)
    .Annotation("SqlServer:TemporalHistoryTableName", "VulnerabilityHistory")
    .Annotation("SqlServer:TemporalHistoryTableSchema", null)
    .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
    .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

该操作在.NET 8及EF Core 8.0.x版本中可正常执行。

测试步骤

先添加乐观并发,再通过独立迁移将实体设为时态表(需先在.NET7/8中完成迁移再升级)。

示例实体代码:

public class Person
{
    public int PersonId { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }

    [Timestamp]
    public byte[] Version { get; set; }
}

报错信息

执行ALTER TABLE [Vulnerability] ALTER COLUMN [Version] rowversion NULL;时触发SqlException,提示:

Cannot alter column 'Version' to be data type timestamp

环境信息

  • EF Core版本:v9.0.1
  • 数据库提供程序:Microsoft.EntityFrameworkCore.SqlServer
  • 目标框架:.NET 9.0
  • 操作系统:Windows 11 Pro
  • IDE:Microsoft Visual Studio Enterprise 2022(64位)- 版本17.12.4

内容的提问来源于stack exchange,提问作者Ogglas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 11:01:12