【问题标题】:EF Core 6.0 temporal tables - Add-Migration - Period property 'Comment.PeriodStart' must be a shadow propertyEF Core 6.0 时态表 - 添加迁移 - 期间属性“Comment.PeriodStart”必须是影子属性
【发布时间】:2022-01-15 02:40:30
【问题描述】:

我们最近将项目升级到Microsoft.EntityFrameworkCore 6.0.0。此版本支持开箱即用的 SQL Server 时态表。

https://devblogs.microsoft.com/dotnet/prime-your-flux-capacitor-sql-server-temporal-tables-in-ef-core-6-0/

https://stackoverflow.com/a/70017768/3850405

自 Entity Framework Core 3.1 以来,我们一直使用时态表,使用此处描述的自定义迁移:

https://stackoverflow.com/a/64776658/3850405

https://stackoverflow.com/a/64244548/3850405

仅仅遵循微软的指南当然行不通,因为默认列名是PeriodStartPeriodEnd,而不是我们的SysStartTimeSysEndTime。历史表名也不匹配。

modelBuilder
    .Entity<Comment>()
    .ToTable("Comments", b => b.IsTemporal());

已创建迁移:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AlterTable(
        name: "Comments")
        .Annotation("SqlServer:IsTemporal", true)
        .Annotation("SqlServer:TemporalHistoryTableName", "CommentsHistory")
        .Annotation("SqlServer:TemporalHistoryTableSchema", null)
        .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
        .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

    migrationBuilder.AddColumn<DateTime>(
        name: "PeriodEnd",
        table: "Comments",
        type: "datetime2",
        nullable: false,
        defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified))
        .Annotation("SqlServer:IsTemporal", true)
        .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
        .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

    migrationBuilder.AddColumn<DateTime>(
        name: "PeriodStart",
        table: "Comments",
        type: "datetime2",
        nullable: false,
        defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified))
        .Annotation("SqlServer:IsTemporal", true)
        .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
        .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");
}

创建自定义转换应按如下所述解决此问题:

modelBuilder
    .Entity<Comment>()
    .ToTable("Comments", tb => tb.IsTemporal(temp =>
    {
        temp.UseHistoryTable("Comments", "History");
        temp.HasPeriodStart("SysStartTime");
        temp.HasPeriodEnd("SysEndTime");
    }));

https://coderedirect.com/questions/540979/how-can-i-use-system-versioned-temporal-table-with-entity-framework

但是,当我这样做时,Add-Migration 命令出现以下错误:

期间属性“Comment.SysStartTime”必须是影子属性。

为了验证我恢复到的任何其他代码没有问题:

modelBuilder
    .Entity<Comment>()
    .ToTable("Comments", b => b.IsTemporal());

然后将public DateTime PeriodStart { get; set; }添加到Comment

然后我收到了错误:

期间属性“Comment.PeriodStart”必须是影子属性。

有没有办法解决这个问题?我们使用我们的SysStartTime 作为最后修改/最后更新的值,它工作得非常好。必须通过EF.Property&lt;DateTime&gt;(comment, "SysStartTime")) 包含它似乎非常不必要,因为该列同时存在于临时表和原始表中。

【问题讨论】:

    标签: c# .net entity-framework entity-framework-core temporal-tables


    【解决方案1】:

    无法在EF Core 6.0 中修复。

    来自实体框架工程经理@ajcvickers:

    不幸的是,没有任何解决方法可以同时允许 使用新的时态表功能,并映射周期 列到非阴影属性。

    https://github.com/dotnet/efcore/issues/26960#issuecomment-991867756

    如果您想在EF Core 7.0 中看到此功能,请在下方投票:

    https://github.com/dotnet/efcore/issues/26463

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-04
      • 2018-08-09
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      相关资源
      最近更新 更多