Skip to content

Commit df68a84

Browse files
Merge pull request #91 from ProfessionalCSharp/efcore6-temporal
temporal tables with EF Core 6.0 #78
2 parents 682e9a3 + 5a1ac46 commit df68a84

File tree

21 files changed

+101
-91
lines changed

21 files changed

+101
-91
lines changed

2_Libs/EFCore/BooksLib/BooksLib.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
11-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
1212
</ItemGroup>
1313

1414

2_Libs/EFCore/ConflictHandling-FirstWins/ConflictHandling-FirstWins.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
</PropertyGroup>-->
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
15+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
1717
<PrivateAssets>all</PrivateAssets>
1818
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1919
</PackageReference>
20-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
20+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
2121
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
2222
</ItemGroup>
2323

2_Libs/EFCore/ConflictHandling-LastWins/ConflictHandling-LastWins.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
</PropertyGroup>-->
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
15+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
1717
<PrivateAssets>all</PrivateAssets>
1818
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1919
</PackageReference>
20-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
20+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
2121
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
2222
</ItemGroup>
2323

2_Libs/EFCore/Cosmos/Cosmos.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
13-
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="6.0.0" />
14-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
12+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="6.0.1" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
1515
<PrivateAssets>all</PrivateAssets>
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>

2_Libs/EFCore/EFCoreSamples.sln

-14
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tracking", "Tracking\Tracki
3131
EndProject
3232
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cosmos", "Cosmos\Cosmos.csproj", "{3456A121-0209-42DC-8B27-C95AC50C1A84}"
3333
EndProject
34-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemporalTableSample", "TemporalTableSample\TemporalTableSample.csproj", "{8319B831-1B27-413C-9BDE-883F00911213}"
35-
EndProject
3634
Global
3735
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3836
Debug|Any CPU = Debug|Any CPU
@@ -199,18 +197,6 @@ Global
199197
{3456A121-0209-42DC-8B27-C95AC50C1A84}.Release|x64.Build.0 = Release|Any CPU
200198
{3456A121-0209-42DC-8B27-C95AC50C1A84}.Release|x86.ActiveCfg = Release|Any CPU
201199
{3456A121-0209-42DC-8B27-C95AC50C1A84}.Release|x86.Build.0 = Release|Any CPU
202-
{8319B831-1B27-413C-9BDE-883F00911213}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
203-
{8319B831-1B27-413C-9BDE-883F00911213}.Debug|Any CPU.Build.0 = Debug|Any CPU
204-
{8319B831-1B27-413C-9BDE-883F00911213}.Debug|x64.ActiveCfg = Debug|Any CPU
205-
{8319B831-1B27-413C-9BDE-883F00911213}.Debug|x64.Build.0 = Debug|Any CPU
206-
{8319B831-1B27-413C-9BDE-883F00911213}.Debug|x86.ActiveCfg = Debug|Any CPU
207-
{8319B831-1B27-413C-9BDE-883F00911213}.Debug|x86.Build.0 = Debug|Any CPU
208-
{8319B831-1B27-413C-9BDE-883F00911213}.Release|Any CPU.ActiveCfg = Release|Any CPU
209-
{8319B831-1B27-413C-9BDE-883F00911213}.Release|Any CPU.Build.0 = Release|Any CPU
210-
{8319B831-1B27-413C-9BDE-883F00911213}.Release|x64.ActiveCfg = Release|Any CPU
211-
{8319B831-1B27-413C-9BDE-883F00911213}.Release|x64.Build.0 = Release|Any CPU
212-
{8319B831-1B27-413C-9BDE-883F00911213}.Release|x86.ActiveCfg = Release|Any CPU
213-
{8319B831-1B27-413C-9BDE-883F00911213}.Release|x86.Build.0 = Release|Any CPU
214200
EndGlobalSection
215201
GlobalSection(SolutionProperties) = preSolution
216202
HideSolutionNode = FALSE

2_Libs/EFCore/Intro/Intro.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
</PropertyGroup>-->
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
15+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
1717
<PrivateAssets>all</PrivateAssets>
1818
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1919
</PackageReference>
20-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
20+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
2121
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
2222
</ItemGroup>
2323

2_Libs/EFCore/LoadingRelatedData/LoadingRelatedData.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
</PropertyGroup>-->
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
15+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
1717
<PrivateAssets>all</PrivateAssets>
1818
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1919
</PackageReference>
20-
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.0" />
21-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
20+
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.1" />
21+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
2222
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
2323
</ItemGroup>
2424

2_Libs/EFCore/MigrationApp/MigrationApp.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
</PackageReference>

2_Libs/EFCore/Models/Models.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
11+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
</PackageReference>
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
1717
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
1818
</ItemGroup>
1919

2_Libs/EFCore/Queries/Queries.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
1313
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
1414
</ItemGroup>
1515

2_Libs/EFCore/Relationships/Relationships.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
</PropertyGroup>-->
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
15+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
1717
<PrivateAssets>all</PrivateAssets>
1818
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1919
</PackageReference>
20-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
20+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
2121
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
2222
</ItemGroup>
2323

2_Libs/EFCore/ScaffoldSample/ScaffoldSample.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
1212
<PrivateAssets>all</PrivateAssets>
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>
15-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
1616
</ItemGroup>
1717

1818
</Project>

2_Libs/EFCore/Tracking/Tracking.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
11+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
</PackageReference>
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
1717
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
1818
</ItemGroup>
1919

2_Libs/EFCore/Transactions/Transactions.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
11+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
</PackageReference>
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
1717
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
1818
</ItemGroup>
1919

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
using System.ComponentModel.DataAnnotations;
22

3-
#if USERECORDS
4-
5-
public record Book(
6-
[property: StringLength(50)] string Title,
7-
[property: StringLength(30)] string? Publisher = default,
8-
int BookId = 0);
9-
10-
#else
3+
namespace TemporalTableSample;
114

125
public class Book
136
{
@@ -23,5 +16,3 @@ public Book(string title, string? publisher = default, int bookId = default)
2316
public string? Publisher { get; set; }
2417
public int BookId { get; set; }
2518
}
26-
27-
#endif
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
using Microsoft.EntityFrameworkCore;
22

3+
namespace TemporalTableSample;
4+
35
public class BooksContext : DbContext
46
{
57
public BooksContext(DbContextOptions<BooksContext> options)
68
: base(options) { }
79

810
protected override void OnModelCreating(ModelBuilder modelBuilder)
911
{
10-
modelBuilder.Entity<Book>().ToTable("Books", b =>
11-
b.IsTemporal()); // creates PeriodStart, PeriodEnd columns, use overload to customize the columns
12+
modelBuilder.Entity<Book>()
13+
.ToTable("Books", b => b.IsTemporal()); // creates PeriodStart, PeriodEnd columns, use overload to customize the columns
1214
}
1315

14-
1516
public DbSet<Book> Books => Set<Book>();
1617
}

2_Libs/EFCore/TemporalTableSample/Program.cs renamed to 5_More/EFCore/TemporalTableSample/Program.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.Configuration;
33
using Microsoft.Extensions.DependencyInjection;
44
using Microsoft.Extensions.Hosting;
5+
using TemporalTableSample;
56

67
using var host = Host.CreateDefaultBuilder(args)
78
.ConfigureServices((context, services) =>
@@ -24,6 +25,7 @@
2425
await runner.ReadBooksAsync();
2526
await runner.QueryBooksAsync();
2627
await runner.UpdateBookAsync();
27-
await runner.TemporalQueryAsync();
28+
await runner.TemporalPointInTimeQueryAsync();
29+
await runner.TemporalAllQueryAsync();
2830
await runner.DeleteBooksAsync();
2931
await runner.DeleteDatabaseAsync();

2_Libs/EFCore/TemporalTableSample/Runner.cs renamed to 5_More/EFCore/TemporalTableSample/Runner.cs

+30-25
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading;
5-
using System.Threading.Tasks;
6-
using Microsoft.EntityFrameworkCore;
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace TemporalTableSample;
74

85
public class Runner
96
{
@@ -84,46 +81,54 @@ await _booksContext.Books
8481
Console.WriteLine();
8582
}
8683

87-
#if USERECORDS
8884
public async Task UpdateBookAsync()
8985
{
9086
Book? book = await _booksContext.Books.FindAsync(1);
87+
Console.WriteLine("Just a short delay before updating...");
88+
await Task.Delay(TimeSpan.FromSeconds(5));
9189

9290
if (book != null)
9391
{
94-
// detach the existing object from the context which allows to attach it with the Update method
95-
_booksContext.Entry(book).State = EntityState.Detached;
96-
Book bookUpdate = book with { Title = "Professional C# and .NET - 2021 Edition" };
97-
_booksContext.Update(bookUpdate);
92+
book.Title = "Professional C# and .NET - 2021 Edition";
9893
int records = await _booksContext.SaveChangesAsync();
9994
Console.WriteLine($"{records} record updated");
100-
10195
}
10296
Console.WriteLine();
10397
}
104-
#else
105-
public async Task UpdateBookAsync()
106-
{
107-
Book? book = _booksContext.Books.Find(1);
10898

109-
if (book != null)
99+
100+
public async Task TemporalPointInTimeQueryAsync()
101+
{
102+
Book? book = await _booksContext.Books.FindAsync(1);
103+
if (book is null) return;
104+
// read shadow property for time
105+
if (_booksContext.Entry(book).CurrentValues["PeriodStart"] is DateTime periodStart)
110106
{
111-
book.Title = "Professional C# and .NET - 2021 Edition";
112-
int records = await _booksContext.SaveChangesAsync();
113-
Console.WriteLine($"{records} record updated");
107+
DateTime previousTime = periodStart.AddSeconds(-4);
108+
var previousBook = await _booksContext.Books
109+
.TemporalAsOf(previousTime)
110+
.TagWith("temporalasof")
111+
.SingleOrDefaultAsync(b => b.BookId == book.BookId);
112+
113+
Console.WriteLine($"actual: {book.BookId}: {book.Title}, {book.Publisher}");
114+
if (previousBook is not null)
115+
{
116+
Console.WriteLine($"earlier: {previousBook.BookId}: {previousBook.Title}, " +
117+
$"{previousBook.Publisher}");
118+
}
114119
}
115-
Console.WriteLine();
116120
}
117-
#endif
118121

119-
public async Task TemporalQueryAsync()
122+
public async Task TemporalAllQueryAsync()
120123
{
121-
await _booksContext.Books.TemporalAll().ForEachAsync(b =>
124+
await _booksContext.Books
125+
.TemporalAll()
126+
.TagWith("temporalall")
127+
.ForEachAsync(b =>
122128
{
123129
var entry = _booksContext.Entry(b);
124130
Console.WriteLine($"{b.Title} {entry.State}");
125131
});
126-
127132
}
128133

129134
public async Task DeleteBooksAsync()

2_Libs/EFCore/TemporalTableSample/TemporalTableSample.csproj renamed to 5_More/EFCore/TemporalTableSample/TemporalTableSample.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
11+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
</PackageReference>
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
1717
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
1818
</ItemGroup>
1919

0 commit comments

Comments
 (0)