C
C#4mo ago
nicke

EF Core how can I access temporal table PeriordStart value to use it for "last modified" information

From the docs: var validFrom = foo.Property<DateTime>("VALID_FROM").CurrentValue; This works and i get the correct DateTime for when the row was last modified. But this is not very nice to do. I would like to be able to access this VALID_FROM via foo.ModifiedUtc My temporal definiton
builder
.ToTable(
"FOO",
b => b.IsTemporal(
tableBuilder =>
{
tableBuilder.HasPeriodStart("VALID_FROM");
tableBuilder.HasPeriodEnd("VALID_TO");
tableBuilder.UseHistoryTable("FOO_HISTORU");
}));
builder
.ToTable(
"FOO",
b => b.IsTemporal(
tableBuilder =>
{
tableBuilder.HasPeriodStart("VALID_FROM");
tableBuilder.HasPeriodEnd("VALID_TO");
tableBuilder.UseHistoryTable("FOO_HISTORU");
}));
What i've tried: Decorate the ModifiedUtc property with a [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
builder
.Property(cc => cc.ModifiedUtc)
.HasColumnName("VALID_FROM")
// Prevent EF from using this property in insert / update statements.
.ValueGeneratedOnAddOrUpdate();
builder
.Property(cc => cc.ModifiedUtc)
.HasColumnName("VALID_FROM")
// Prevent EF from using this property in insert / update statements.
.ValueGeneratedOnAddOrUpdate();
But this just ends in an exception: Microsoft.Data.SqlClient.SqlException : Invalid column name 'VALID_FROM1'. So EF seems to enumerate one of my mappings by adding a 1 after the column name.
1 Reply
nicke
nicke4mo ago
If anyone ever runs into this. Here was the best solution: https://github.com/dotnet/efcore/issues/26463#issuecomment-1984046675
GitHub
Allow temporal Period properties to be mapped to CLR properties (i....
(.NET 6, EF Core 6 RC2/latest) I generate the fields in an entity which are mapped to the period fields in a temporal table as readonly properties and map them as normal fields, however when using ...