Test if table IsTemporal in EF Core 6.0

Viewed 38

EF Core 6.0 now supports IsTemporal() which allows you to use an MS-SQL history table. I use DbContext.OnBeforeSaving() to validate certain fields before I commit the data. If the table is temporal then I would like to save the UserId of the person who updated this line.

How do I test if the table is temporal? (i.e. by psudo-code below)

foreach (var entry in ChangeTracker.Entries<MyBaseEntityType>())
{
   // if (entry is Temporal)
   //    set entry.Entity.UserID from IHttpContextAccessor.HttpContext.User
}
1 Answers

EF Core 6.0 now supports IsTemporal() which allows you to use an MS-SQL history table

I guess you have in mind IsTemporal() fluent configuration API like

modelBuilder.Entity<MyTable>().ToTable(tableBuilder =>
    tableBuilder.IsTemporal());

For entities configured that way, there is corresponding IsTemporal() metadata extension method, e.g. (requires reference to Microsoft.EntityFrameworkCore.SqlServer assembly)

foreach (var entry in ChangeTracker.Entries<MyBaseEntityType>())
{
    if (entry.Metadata.IsTemporal())
    {
        // Do something
    }
}
Related