Skip to content

Renaming PeriodStart and PeriodEnd columns of a temporal table causes them to be swapped #29902

@orty

Description

@orty

Hi,

I just discovered that, in my database, on 3 temporal tables, the meaning of the PeriodStart and PeriodEnd column has been kind of swapped after renaming them (to be compliant with our naming convention).
As a result, dates in the new PeriodStart-like column are greater than the dates in the PeriodEnd one.

Repro steps:

Having this entity model:

public class Contact : IEntity
{
    /// <inheritdoc />
    public Guid Id { get; set; }
}

Having this in DbContext:

modelBuilder.Entity<Contact>(
    entity =>
    {
        entity.ToTable(
            builder =>
            {
                builder.IsTemporal();
            });

        entity.HasKey(c => c.Id).HasName("ConId");
    });

Generates the following migration Up():

migrationBuilder.AlterTable(
    name: "Contacts")
    .Annotation("SqlServer:IsTemporal", true)
    .Annotation("SqlServer:TemporalHistoryTableName", "ContactsHistory")
    .Annotation("SqlServer:TemporalHistoryTableSchema", null)
    .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
    .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");
    
migrationBuilder.AddColumn<DateTime>(
    name: "PeriodEnd",
    table: "Contacts",
    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: "Contacts",
    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");

Then, if we are to rename the columns to be compliant with the Con prefix trigram, we add this to the DbContext:

modelBuilder.Entity<Contact>(
    entity =>
    {
        entity.ToTable(
            builder =>
            {
                builder.IsTemporal(
                    tableBuilder =>
                    {
                        tableBuilder.HasPeriodStart("ConValidFrom");
                        tableBuilder.HasPeriodEnd("ConValidTo");
                    });
            });
  
        entity.HasKey(c => c.Id).HasName("ConId");
    });

And this is the resulting migration Up() generated method:

migrationBuilder.RenameColumn(
    name: "PeriodStart",
    table: "Contacts",
    newName: "ConValidTo");

migrationBuilder.RenameColumn(
    name: "PeriodEnd",
    table: "Contacts",
    newName: "ConValidFrom");

migrationBuilder.AlterTable(
    name: "Contacts")
    .Annotation("SqlServer:IsTemporal", true)
    .Annotation("SqlServer:TemporalHistoryTableName", "ContactsHistory")
    .Annotation("SqlServer:TemporalHistoryTableSchema", null)
    .Annotation("SqlServer:TemporalPeriodEndColumnName", "ConValidTo")
    .Annotation("SqlServer:TemporalPeriodStartColumnName", "ConValidFrom")
    .OldAnnotation("SqlServer:IsTemporal", true)
    .OldAnnotation("SqlServer:TemporalHistoryTableName", "ContactsHistory")
    .OldAnnotation("SqlServer:TemporalHistoryTableSchema", null)
    .OldAnnotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
    .OldAnnotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

Here, it seems to me that the renaming of the columns kind of "swaps" them, isn't it ?

Thanks.

Include provider and version information

EF Core version: 6.0.5
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Win 11
IDE: Visual Studio 2022 17.3.6

Metadata

Metadata

Assignees

Type

No fields configured for Bug.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions