You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow temporal Period properties to be mapped to CLR properties (i.e. not shadow properties) #26463
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 them in a query I get:
System.InvalidOperationException : Period property 'Employee.ValidFrom' must be a shadow property.
at Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerModelValidator
Why do the period properties have the requirement to be shadow properties? I could generate them as such but IMHO it sucks for the user as to use them they have to fall back to string based names for the fields...
Reacted by Oscar Andersson, Exitare, Dragos Durlut, Muhammad Azeez, Sveinung, MaksHl, Reveon, Mohamed Dawood, Adam, Hannes Sachsenhofer and 69 more
@FransBouma We think they make more sense as shadow properties since most uses of the entity do not need the period properties. (Essentially, the same reason they are hidden by default in the table.) That being said, we will enable mapping them to non-shadow properties in 7.0.
@ajcvickers 👍
I have it working now, having the fields in the entity definition but not mapped in the modelbuilder. The main reason I did keep them in my own framework (instead of hiding them) is also because when you fetch a set of these entities, you can use the values for the period e.g. in a UI or other means. But I admit, it's a subjective thing: do they belong to the entity or are they strictly there to help the DB do its bookkeeping.
Based on a solution from user @StevenRasmussen (from this thread ) I figured out a hack to map shadow properties to fields in the entity.
Part of the inspiration came from user @FransBouma as well.
The solution is not perfect but it does the job without having to alter too much of the code. This is done over efcore 6.0.1 version.
Code might not work on an upgrade.
changed the title [-]Why are Temporal Period properties required to be shadow properties?[/-][+]Allow temporal Period properties to be mapped to CLR properties (i.e. not shadow properties)[/+]on Jan 25, 2022
I've added a demo for this implementation on a temporal tables demo solution.
It can be found here (branch period-properties).
I have renamed PeriodStart -> FromSysDate & PeriodEnd -> ToSysDate for easier debugging. (I had some issues with following the correct columns)
A note is that you have to be careful with migrations.
In some instances the [NotMapped] attribute does not help and has to be temporarily commented out, generate the migration and then put it back in.
It makes perfect sense to me that I would present status messages throughout my UI based on the Period column values:
ex: $"Last edited by {row.ModifiedBy} on {row.PeriodStart}"
While accessing shadow properties is workable for simple scenarios, if I'm doing a more complex history query where for example, I need to find the last edit date of the most recently edited row from a navigation property, there is no practical way to state this query in EF/LINQ based on shadow properties of a navigation target.
I don't know if this needs to be said, but to be crystal of what I'm expecting, please allow mapping Period columns EXACTLY like all other mapped properties on an entity (from a query perspective).
P.S.
and while we're at it, why not take that last statement to its logical end and make the fluent API for mapping temporal tables and period columns match with mapping any other table/property:
I don't know if this needs to be said, but to be crystal of what I'm expecting, please allow mapping Period columns EXACTLY like all other mapped properties on an entity (from a query perspective).
That's what I argued indeed in the startpost :)
Reacted by Gerasimos "Gerry" Raptis, wdhenrik, FB and Ian Kemp
In my database I have defined the temporal columns as PeriodStart and PeriodEnd.
In the DbContext, the entity is set with IsTemporal and UseHistoryTable.
In the model class, I have added my LastModifiedTimestamp property which maps to PeriodStart:
[Column("PeriodStart")]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime LastModifiedTimestamp { get; init; }
The value for LastModifiedTimestamp (PeriodStart) is returned in both tracked and untracked queries, and I can filter on and select LastModifiedTimestamp in my LINQ.
@burtonrodman that looks about right and the same, except our database column names differ.
Are you absolutely sure that you don't already have a property named SysStartTime and SysEndTime in your model? Or that you haven't already mapped another property to your SysStartTime and SysEndTime column names? I have seen that error with a 1 suffix before if there are two mappings to the same column.
One other thing to try out of desperation would be to change your property names to anything but 'PeriodStart' and 'PeriodEnd' (in case EF is internally making them special)
This issue is in the Backlog milestone. This means that it is not planned for the next release. We will re-assess the backlog following this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources, see Release planning process. Make sure to vote (👍) for this issue if it is important to you.
In NET10.0, IEntityMaterializerSource has been replaced by IStructuralTypeMaterializerSource.
In my migration from NET8 to NET10 I have created a TemporalStructuralTypeMaterializerSource class.
Unfortunately, I could not overload the original StructuralTypeMaterializerSource class.
I highly encourage the EF Core team to take this up in the next release. As it stands, this is a fatal flaw in the design of EF temporal tables implementation.
Row validity dates are not SQL metadata; they are first-class table columns and essential data in any imaginable enterprise system. The fact that they are HIDDEN by convention in SQL is so that they don't break legacy SELECT * and bare INSERT statements, not because they are "hidden" from the user. Exposing these columns as change tracker metadata is not a workable solution in large systems were we often use .AsNoTracking() on large result sets.
Activity
ajcvickers commentedon Oct 26, 2021
@FransBouma We think they make more sense as shadow properties since most uses of the entity do not need the period properties. (Essentially, the same reason they are hidden by default in the table.) That being said, we will enable mapping them to non-shadow properties in 7.0.
FransBouma commentedon Oct 26, 2021
@ajcvickers 👍
I have it working now, having the fields in the entity definition but not mapped in the modelbuilder. The main reason I did keep them in my own framework (instead of hiding them) is also because when you fetch a set of these entities, you can use the values for the period e.g. in a UI or other means. But I admit, it's a subjective thing: do they belong to the entity or are they strictly there to help the DB do its bookkeeping.
dragos-durlut commentedon Jan 7, 2022
Based on a solution from user @StevenRasmussen (from this thread ) I figured out a hack to map shadow properties to fields in the entity.
Part of the inspiration came from user @FransBouma as well.
The solution is not perfect but it does the job without having to alter too much of the code. This is done over efcore 6.0.1 version.
Code might not work on an upgrade.
The interface I used to keep track of things
An example of the implemented entity, do not forget to add
NotMappedto the interface members.The constants class so we do not have to hardcode everything:
The overload of
EntityMaterializerSourceand implementation ofIEntityMaterializerSourceThe setup in
DbContext:Not sure if necessary in Startup:
How to setup the entity tables:
On querying the data, you will have to still use
EF.Property:That is about it. I will add more if I find any bugs.
[-]Why are Temporal Period properties required to be shadow properties?[/-][+]Allow temporal Period properties to be mapped to CLR properties (i.e. not shadow properties)[/+]dragos-durlut commentedon Jan 25, 2022
I've added a demo for this implementation on a temporal tables demo solution.
It can be found here (branch period-properties).
I have renamed
PeriodStart->FromSysDate&PeriodEnd->ToSysDatefor easier debugging. (I had some issues with following the correct columns)A note is that you have to be careful with migrations.
In some instances the
[NotMapped]attribute does not help and has to be temporarily commented out, generate the migration and then put it back in.Then column names have to be swapped:
You can see the list of changes here (commit 1597590)
burtonrodman commentedon Feb 2, 2022
Maybe I'm using temporal tables wrong ;)
It makes perfect sense to me that I would present status messages throughout my UI based on the Period column values:
ex: $"Last edited by {row.ModifiedBy} on {row.PeriodStart}"
While accessing shadow properties is workable for simple scenarios, if I'm doing a more complex history query where for example, I need to find the last edit date of the most recently edited row from a navigation property, there is no practical way to state this query in EF/LINQ based on shadow properties of a navigation target.
I don't know if this needs to be said, but to be crystal of what I'm expecting, please allow mapping Period columns EXACTLY like all other mapped properties on an entity (from a query perspective).
P.S.
and while we're at it, why not take that last statement to its logical end and make the fluent API for mapping temporal tables and period columns match with mapping any other table/property:
FransBouma commentedon Feb 2, 2022
@burtonrodman
That's what I argued indeed in the startpost :)
channeladam commentedon Mar 14, 2022
I appear to have a working solution.
In my database I have defined the temporal columns as PeriodStart and PeriodEnd.
In the DbContext, the entity is set with IsTemporal and UseHistoryTable.
In the model class, I have added my LastModifiedTimestamp property which maps to PeriodStart:
The value for LastModifiedTimestamp (PeriodStart) is returned in both tracked and untracked queries, and I can filter on and select LastModifiedTimestamp in my LINQ.
burtonrodman commentedon Mar 15, 2022
@channeladam would you be able to provide a full example?
When I try your solution, I receive the following error doing a simple select from the table:
For historical reasons, my period columns are named SysStartTime and SysEndTime, which I have specified like this:
I have added properties like yours to my entity class:
Any suggestions would be appreciated.
btw, SysStartTime and SysEndTime in the above examples are string constants.
channeladam commentedon Mar 16, 2022
@burtonrodman that looks about right and the same, except our database column names differ.
Are you absolutely sure that you don't already have a property named SysStartTime and SysEndTime in your model? Or that you haven't already mapped another property to your SysStartTime and SysEndTime column names? I have seen that error with a 1 suffix before if there are two mappings to the same column.
One other thing to try out of desperation would be to change your property names to anything but 'PeriodStart' and 'PeriodEnd' (in case EF is internally making them special)
32 remaining items
AndriySvyryd commentedon Aug 19, 2025
This issue is in the Backlog milestone. This means that it is not planned for the next release. We will re-assess the backlog following this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources, see Release planning process. Make sure to vote (👍) for this issue if it is important to you.
dragos-durlut commentedon Mar 13, 2026
In NET10.0,
IEntityMaterializerSourcehas been replaced byIStructuralTypeMaterializerSource.In my migration from NET8 to NET10 I have created a
TemporalStructuralTypeMaterializerSourceclass.Unfortunately, I could not overload the original StructuralTypeMaterializerSource class.
The commit is here
nlaslett commentedon Apr 14, 2026
I highly encourage the EF Core team to take this up in the next release. As it stands, this is a fatal flaw in the design of EF temporal tables implementation.
Row validity dates are not SQL metadata; they are first-class table columns and essential data in any imaginable enterprise system. The fact that they are
HIDDENby convention in SQL is so that they don't break legacySELECT *and bareINSERTstatements, not because they are "hidden" from the user. Exposing these columns as change tracker metadata is not a workable solution in large systems were we often use.AsNoTracking()on large result sets.See #38093 for more.
Allow temporal period properties to be mapped to CLR properties
Allow temporal period properties to be mapped to CLR properties (#38110)