EFCore.TimeTraveler
0.26.1
dotnet add package EFCore.TimeTraveler --version 0.26.1
NuGet\Install-Package EFCore.TimeTraveler -Version 0.26.1
<PackageReference Include="EFCore.TimeTraveler" Version="0.26.1" />
paket add EFCore.TimeTraveler --version 0.26.1
#r "nuget: EFCore.TimeTraveler, 0.26.1"
// Install EFCore.TimeTraveler as a Cake Addin
#addin nuget:?package=EFCore.TimeTraveler&version=0.26.1
// Install EFCore.TimeTraveler as a Cake Tool
#tool nuget:?package=EFCore.TimeTraveler&version=0.26.1
Background
EF Core does not natively support querying from the history of temporal tables. You may query a single temporal table using .FromSqlRaw(...)
or .FromSqlInterpolated(...)
. Multiple temporal tables can be queried using the same raw SQL functionality with LINQ Join. Additionally, the EfCoreTemporalTable library provides a nice syntax for this functionality.
However, any related data from Include(...)
or navigation properties is not able to be queried from temporal history with EF Core. The problem I am trying to solve is that I have a good amount of LINQ queries and other code written and tested, but now a requirement has come in requiring loading the same set of data at a prior System Time. The Point-in-time analysis (time travel) usage scenario for SQL Server Temporal Tables pretty well described what I am wanting to do.
Prerequisites
- EF Core 3.1 (Supports .NETStandard 2.0)
- SQL Server 2016 or higher or Azure SQL (For Temporal Table Support)
Assumptions
- EF ALWAYS generates SQL with table names surrounded by square brackets.
- EF ALWAYS uses a table alias in generated SQL, so that the table name does not get repeated in the SQL unless joining to the same table.
Restrictions
Since history is immutable, all EF queries within a TemporalQuery.AsOf(targetTime)
block must use .AsNoTracking()
. This avoids the DBContext getting confused and caching prior state data as the current state of the data. In a future release, the disabling of change tracking for temporal queries may be automatic if I ever figure out the best way to do this. PR's are welcome.
Example Usage
EF Core Mapping
var appleEntity = modelBuilder.Entity<Apple>()
.EnableTemporalQuery();
appleEntity.HasKey(apple => apple.Id);
var wormEntity = modelBuilder.Entity<Worm>()
.EnableTemporalQuery();
wormEntity.HasOne(worm => worm.Apple)
.WithMany(apple => apple.Worms)
.HasForeignKey(worm => worm.AppleId);
EF Core LINQ Query
var appleCurrentState = await context.Apples
.Include(apple => apple.Worms)
.Where(a => a.Id == appleId)
.AsNoTracking()
.SingleAsync();
appleCurrentState.Worms.Count().Should().Be(3);
using (TemporalQuery.AsOf(ripeAppleTime))
{
var applePriorState = await context.Apples
.Include(apple => apple.Worms)
.Where(a => a.Id == appleId)
.AsNoTracking()
.SingleAsync();
applePriorState.Worms.Count().Should().Be(0);
}
More Complicated Example
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.EntityFrameworkCore.Abstractions (>= 3.1.0)
- Microsoft.EntityFrameworkCore.Relational (>= 3.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|