Skip to content

Simplify configuration for temporal tables with owned types #29303

@ajcvickers

Description

@ajcvickers

Ideally, of the owning entity type is configured as temporal, then owned entities mapped to the same table should not need additional configuration to make use of the temporal table, with the same PeriodStart and PeriodEnd columns.

For example, this configuration should be sufficient:

modelBuilder
    .Entity<Employee>()
    .ToTable("Employees", tableBuilder => tableBuilder.IsTemporal())
    .OwnsOne(employee => employee.Info);

But throws:

Entity type 'EmployeeInfo' should be marked as temporal because it shares table mapping with another entity that has been marked as temporal. Alternatively, other entity types that share the same table must be non-temporal.

Attempting to fix this:

modelBuilder
    .Entity<Employee>()
    .ToTable("Employees", tableBuilder => tableBuilder.IsTemporal())
    .OwnsOne(employee => employee.Info)
    .ToTable("Employees", tableBuilder => tableBuilder.IsTemporal());

Results in:

When multiple temporal entities are mapped to the same table, their period start properties must map to the same column. Issue happens for entity type 'EmployeeInfo' with period property 'PeriodStart' which is mapped to column 'EmployeeInfo_PeriodStart'. Ex
pected period column name is 'PeriodStart'.

Attempting the set the column name explicit on one or other type doesn't help. So the simplest config that I can come up with that works is:

modelBuilder
    .Entity<Employee>()
    .ToTable(
        "Employees",
        tableBuilder =>
        {
            tableBuilder.IsTemporal();
            tableBuilder.Property<DateTime>("PeriodStart").HasColumnName("PeriodStart");
            tableBuilder.Property<DateTime>("PeriodEnd").HasColumnName("PeriodEnd");
        })
    .OwnsOne(
        employee => employee.Info,
        ownedBuilder => ownedBuilder.ToTable(
            "Employees",
            tableBuilder =>
            {
                tableBuilder.IsTemporal();
                tableBuilder.Property<DateTime>("PeriodStart").HasColumnName("PeriodStart");
                tableBuilder.Property<DateTime>("PeriodEnd").HasColumnName("PeriodEnd");
            }));

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions