-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataContext.cs
50 lines (44 loc) · 1.44 KB
/
DataContext.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
39
40
41
42
43
44
45
46
47
48
49
50
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace EFCore_TrackedEventCriticalSection
{
public class TestDataContext : DbContext
{
public TestDataContext(DbContextOptions options) : base(options)
{
this.ChangeTracker.Tracked += ChangeTracker_Tracked;
}
private void ChangeTracker_Tracked(object sender, Microsoft.EntityFrameworkCore.ChangeTracking.EntityTrackedEventArgs e)
{
if (e.Entry.Entity is Employee employee)
{
var devices = employee.Devices;
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EmployeeDevice>()
.HasOne<Employee>(a => a.Employee)
.WithMany(p => p.Devices)
.HasForeignKey(a => a.EmployeeId)
.IsRequired(true);
base.OnModelCreating(modelBuilder);
}
}
public class Employee
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<EmployeeDevice> Devices { get; set; }
}
public class EmployeeDevice
{
[Key]
public int Id { get; set; }
public int EmployeeId { get; set; }
public string Device { get; set; }
public virtual Employee Employee { get; set; }
}
}