Skip to content

SQL Server temporal tables #4693

Closed
Closed

Description

@shivajiraot

Does EF Core support code first approach for Temporal tables?
Looking to find a way to use Temporal tables with EF Core code first approach. Please guide me to the article if any existing already.
Thank you.

Activity

  1. rowanmiller commented on Mar 4, 2016

    @rowanmiller
    Contributor

    For the most part, the fact that a table is temporal is opaque to the data layer - so from that point of view you should be fine to connect to an existing database with temporal tables.

    Here are some limitations though, that we could address in EF:

    • If you wanted to query a past state of the table you would need to use a raw SQL query.
    • There is no way to make a table temporal in the model/migrations. So if you are using EF to create the database then you would need to hand edit the migration and use Sql(string) to make the table temporal.
  2. changed the title [-]Temporal Tables[/-] [+]Support Temporal Tables[/+] on Mar 8, 2016
  3. added this to the Backlog milestone on Mar 8, 2016
  4. DavidBainbridgeRIT commented on Jun 7, 2016

    @DavidBainbridgeRIT

    I second this as important. Temporal tables are fantastic. They cut out a lot of boiler plate but I need that information in the front end just as easily as in the back end.

  5. R00iBaard commented on Sep 16, 2016

    @R00iBaard

    Agree, I would love to be able to set this up all via EF code first.

  6. reberinformatik commented on Sep 21, 2016

    @reberinformatik

    👍 See also #2229

  7. lucasmaj commented on Sep 28, 2016

    @lucasmaj

    I was absolutely bummed out to find out that currently there is no obvious way to query past state using lambda expressions. Is there any way to sneak in "AS OF <date_time>" into a query? (in EF6)

  8. divega commented on Sep 28, 2016

    @divega
    Contributor

    @lucasmaj have you tried raw SQL queries as a workaround as explained above? E.g. in EF6 use SqlQuery() and FromSql() with EF Core. The latter is composable in LINQ.

  9. lucasmaj commented on Sep 28, 2016

    @lucasmaj

    I haven't tried raw SQL. If EF6 had SqlQuery composable with LINQ it would do the trick for now. There is no way I would go with pure sql queries and abandon LINQ. Unless there could be a way for LINQ to pure sql back to entities in an automatic fashion.

    Is there really no way to augment conversion of Expressions to SQL string so that some custom expression would translate to "AS OF" etc?

    Thank you nonetheless.

  10. R00iBaard commented on Apr 20, 2017

    @R00iBaard

    Any update on this? It would be very nice to have this 👍

  11. ajcvickers commented on Apr 20, 2017

    @ajcvickers
    Contributor

    @R00iBaard This issue is in the Backlog milestone. This means that it is not going to happen for the 2.0 release. We will re-assess the backlog following the 2.0 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.

  12. ntimmerman commented on Jun 14, 2017

    @ntimmerman

    +1, I would like to use this in the very near future for a new enterprise doc automation app that requires an audit history on every screen. Does anyone have any more information about the work around's for now?

  13. pray1997 commented on Jun 15, 2017

    @pray1997

    +1

    We need it too!

    Or is.there any workaround, maybe something like intercepting?

  14. 119 remaining items

  15. added this to the 6.0.0 milestone on Oct 28, 2020
  16. changed the title [-]Support Temporal Tables[/-] [+]Support temporal tables[/+] on Feb 10, 2021
  17. maumar commented on Jul 29, 2021

    @maumar
    Contributor

    Initial support for Temporal Tables has been added here: e7c0b9d (model/metadata part) and here: 4b25a88 (query part) and will be available in the next preview (Preview 8), as well as in current nightly bits.

    Usage:

    Mapping entity to a temporal table can be done in the OnModelCreating, like this:

    modelBuilder.Entity<MyTemporalEntity>().ToTable(tb => tb.IsTemporal());

    additional configuration is also supported - history table name/schema, names for period start and period end columns

    modelBuilder.Entity<MyTemporalEntity>().ToTable(tb => tb.IsTemporal(ttb =>
    {
        ttb.HasPeriodStart("SystemTimeStart");
        ttb.HasPeriodEnd("SystemTimeEnd");
        ttb.WithHistoryTable("MyHistoryTable", "mySchema");
    }));

    Migrations are supported so existing entities can be converted to temporal.

    Querying:

    var myDate = new DateTime(2020, 1, 1);
    context.MyTemporalEntities.TemporalAsOf(myDate).Where(e => e.Id < 10);

    Supported operations: TemporalAsOf, TemporalAll, TemporalBetween, TemporalFromTo, TemporalContainedIn.

    Some limitations and considerations

    • Queries that use temporal operations are always marked as NoTracking. Multiple entities with the same key could be returned from such queries and EF would not be able to resolve their identities properly otherwise.

    • Temporal operations are supported directly on DbSet, rather than IQueryable. In case of inheritance, they can't be applied on OfType operation. Instead, use:

    context.Set<MyDerivedEntity>().TemporalAsOf(...); 
    • Navigation expansion is only supported for AsOf operation, since it's the only temporal operation that guarantees consistency of the result graph. For other temporal operations navigations must be created manually using Join.

    • When expanding navigation, the target entity must also be mapped to temporal table. Temporal operation gets propagated from source to the target. Navigating from temporal to non-temporal entity is not supported.

    context.Customers.TemporalAsOf(new DateTime(2020, 1, 1)).Select(c => c.Orders)

    will return customers and their orders as of Jan 1st 2020. Temporal operation gets applied to customers AND orders automatically.

    Note:
    This thread is already very long, for any feedback, questions, bug reports etc. please go here as a starting point: dotnet/EntityFramework.Docs#3353

  18. modified the milestones: 6.0.0, on Aug 12, 2021
  19. changed the title [-]Support temporal tables[/-] [+]SQL Server temporal tables[/+] on Sep 16, 2021
  20. modified the milestones: , 6.0.0 on Nov 8, 2021
  21. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions