EF Core Temporal Table on Owned Entity

EF Core Temporal Table on Owned Entity

I have an EF Core defined table, and a child entity that is configured using OwnsOne without using a secondary table. The properties of the child entity are stored in the same table as the parent entity. When I set the parent entity to temporal, I cannot see a way to set the child entity to temporal as well.

EF Core gives me this error if I don't set child as temporal:

Unable to create a 'DbContext' of type 'DataContext'.

The exception 'Entity type 'Parent.ChildProp#Type' 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.'
was thrown while attempting to create an instance.

For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

My EF Core config looks like this:

builder.ToTable("ParentTable", t => t.IsTemporal());

builder
    .OwnsOne(t => t.ChildProp, childBuilder =>
    {
        childBuilder 
            .Property(t => t.Amount)
            .IsRequired()
            .HasPrecision(18, 2)
            .HasColumnName("Parent_Amount");

        childBuilder 
            .Property(t => t.Currency)
            .IsRequired()
            .HasEnumConversion()
            .HasColumnName("Parent_Currency");
    });

Any help is appreciated, thanks!

Answer

Have you tried adding a childBuilder.ToTable(x => x.IsTemporal()); after defining the properties inside OwnsOne?

I believe that, by doing that, you inform ef that childProp is also temporary.

I got that from Read more.

Enjoyed this article?

Check out more content on our blog or follow us on social media.

Browse more articles