dotnet / dotnet/efcore

Simplify configuration for temporal tables with owned types

Open
#29,303 5 comments 48 reactions 0 assignees View on GitHub
area-model-building area-temporal-tables
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

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:

```C#
modelBuilder
.Entity()
.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:

```C#
modelBuilder
.Entity()
.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:

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.