11DateTime _updateDateTime = DateTime . UtcNow ;
2+ using DemoContext context = new ( ) ;
23
34do
45{
1112 Console . ResetColor ( ) ;
1213} while ( true ) ;
1314
14-
1515async Task SwitchboardAsync ( )
1616{
1717 Console . Clear ( ) ;
@@ -76,21 +76,18 @@ async Task SwitchboardAsync()
7676async 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
9189async 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
103100async 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
114110void 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
133128async 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
148142async 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