Skip to content

Commit 8707bd9

Browse files
committed
Cleaned up the EFCore demo project
1 parent 9fca2d1 commit 8707bd9

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.3.32714.290
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCoreDemo", "EFCoreDemo.csproj", "{4F1A9564-41B6-4266-88D9-BBEBACA02A31}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCoreDemo", "EFCoreDemo\EFCoreDemo.csproj", "{78D40981-A405-440C-89FF-0DFBBD5C464C}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1010
Debug|Any CPU = Debug|Any CPU
1111
Release|Any CPU = Release|Any CPU
1212
EndGlobalSection
1313
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14-
{4F1A9564-41B6-4266-88D9-BBEBACA02A31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15-
{4F1A9564-41B6-4266-88D9-BBEBACA02A31}.Debug|Any CPU.Build.0 = Debug|Any CPU
16-
{4F1A9564-41B6-4266-88D9-BBEBACA02A31}.Release|Any CPU.ActiveCfg = Release|Any CPU
17-
{4F1A9564-41B6-4266-88D9-BBEBACA02A31}.Release|Any CPU.Build.0 = Release|Any CPU
14+
{78D40981-A405-440C-89FF-0DFBBD5C464C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{78D40981-A405-440C-89FF-0DFBBD5C464C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{78D40981-A405-440C-89FF-0DFBBD5C464C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{78D40981-A405-440C-89FF-0DFBBD5C464C}.Release|Any CPU.Build.0 = Release|Any CPU
1818
EndGlobalSection
1919
GlobalSection(SolutionProperties) = preSolution
2020
HideSolutionNode = FALSE

Demos/EFCoreDemo/Program.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
DateTime _updateDateTime = DateTime.UtcNow;
2+
using DemoContext context = new();
23

34
do
45
{
@@ -11,7 +12,6 @@
1112
Console.ResetColor();
1213
} while (true);
1314

14-
1515
async Task SwitchboardAsync()
1616
{
1717
Console.Clear();
@@ -76,21 +76,18 @@ async Task SwitchboardAsync()
7676
async Task InitializeDatabase()
7777
{
7878
Console.WriteLine("Initializing database...");
79-
Employee graham = new() { FirstName = "Leanney", LastName = "Graham", Department = "HR" };
80-
Employee howell = new() { FirstName = "Ervint", LastName = "Howell", Department = "HR" };
81-
Employee baucho = new() { FirstName = "Clementine", LastName = "Baucho", Department = "Legal" };
82-
Employee lebsack = new() { FirstName = "Patriciari", LastName = "Lebsack", Department = "IT" };
83-
Employee dietrich = new() { FirstName = "Chelst", LastName = "Dietrich", Department = "IT" };
84-
Employee weissnat = new() { FirstName = "Kurt", LastName = "Weissnat", Department = "IT" };
85-
using DemoContext context = new();
86-
await context.AddRangeAsync(graham, howell, baucho, lebsack, dietrich, weissnat);
79+
await context.Employees.AddAsync(new() { FirstName = "Aurelia", LastName = "Bogart", Department = "HR" });
80+
await context.Employees.AddAsync(new() { FirstName = "Faith", LastName = "Mitchell", Department = "HR" });
81+
await context.Employees.AddAsync(new() { FirstName = "Jason", LastName = "Fielder", Department = "Legal" });
82+
await context.Employees.AddAsync(new() { FirstName = "Chris", LastName = "Dean", Department = "IT" });
83+
await context.Employees.AddAsync(new() { FirstName = "William", LastName = "Gott", Department = "IT" });
84+
await context.Employees.AddAsync(new() { FirstName = "Annie", LastName = "Smith", Department = "IT" });
8785
context.SaveChanges();
8886
Console.WriteLine("Database initialization complete");
8987
}
9088

9189
async Task UpdateDepartmentAsync(int employeeId, string department)
9290
{
93-
using DemoContext context = new();
9491
Employee? employee = await context.Employees.FindAsync(employeeId);
9592
if (employee is not null)
9693
{
@@ -102,7 +99,6 @@ async Task UpdateDepartmentAsync(int employeeId, string department)
10299

103100
async Task DeleteEmployeeAsync(int employeeId)
104101
{
105-
using DemoContext context = new();
106102
Employee? employee = await context.Employees.FindAsync(employeeId);
107103
if (employee is not null)
108104
{
@@ -113,7 +109,6 @@ async Task DeleteEmployeeAsync(int employeeId)
113109

114110
void PrintEmployeeHistory(int employeeId)
115111
{
116-
using DemoContext context = new();
117112
var history = context.Employees.TemporalAll().Where(emp => emp.Id == 2)
118113
.OrderByDescending(emp => EF.Property<DateTime>(emp, "PeriodStart"))
119114
.Select(emp => new
@@ -132,7 +127,6 @@ void PrintEmployeeHistory(int employeeId)
132127

133128
async Task PrintEmployeeAsOfDateTimeAsync(int employeeId, DateTime dateTime)
134129
{
135-
using DemoContext context = new();
136130
Employee? employee = await context.Employees.FindAsync(employeeId);
137131
Employee? historicalEmployee = await context.Employees
138132
.TemporalAsOf(dateTime)
@@ -147,9 +141,15 @@ async Task PrintEmployeeAsOfDateTimeAsync(int employeeId, DateTime dateTime)
147141

148142
async Task RestoreDeletedEmployeeAsync(int employeeId)
149143
{
150-
using DemoContext context = new();
151-
DateTime delTimestamp = await context.Employees.TemporalAll().Where(x => x.Id == employeeId).OrderBy(x => EF.Property<DateTime>(x, "PeriodEnd")).Select(x => EF.Property<DateTime>(x, "PeriodEnd")).LastAsync();
152-
Employee deletedEmployee = await context.Employees.TemporalAsOf(delTimestamp.AddMilliseconds(-1)).SingleAsync(x => x.Id == employeeId);
144+
DateTime delTimestamp = await context.Employees
145+
.TemporalAll()
146+
.Where(x => x.Id == employeeId)
147+
.OrderBy(x => EF.Property<DateTime>(x, "PeriodEnd"))
148+
.Select(x => EF.Property<DateTime>(x, "PeriodEnd"))
149+
.LastAsync();
150+
Employee deletedEmployee = await context.Employees
151+
.TemporalAsOf(delTimestamp.AddMilliseconds(-1))
152+
.SingleAsync(x => x.Id == employeeId);
153153
await context.AddAsync(deletedEmployee);
154154
context.Database.OpenConnection();
155155
context.Database.ExecuteSqlInterpolated($"SET IDENTITY_INSERT Employee ON");

0 commit comments

Comments
 (0)