-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPeopleContext.cs
38 lines (30 loc) · 916 Bytes
/
PeopleContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
public class PeopleContext : DbContext
{
public PeopleContext()
{
SavingChanges += UpdateShadowProperties;
}
private void UpdateShadowProperties(object sender, SavingChangesEventArgs e)
{
foreach (var entry in ChangeTracker.Entries()
.Where(entry => entry.Entity is Person))
{
entry.Property("LastUpdated").CurrentValue = DateTime.Now;
}
}
public DbSet<Person> People { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=MigrationBundleDemo.db");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>().Property<DateTime>("LastUpdated");
}
}