Repository navigation
Expand file tree
/
Copy pathSampleContext.cs
More file actions
158 lines (146 loc) · 5.03 KB
/
Copy pathSampleContext.cs
File metadata and controls
158 lines (146 loc) · 5.03 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
namespace Sample.Model;
public sealed class SampleContext(DbContextOptions<SampleContext> options) :
DbContext(options)
{
// begin-snippet: dbContext
public DbSet<Department> Departments => Set<Department>();
public DbSet<Employee> Employees => Set<Employee>();
public DbSet<Order> Orders => Set<Order>();
public DbSet<EmployeeSummary> EmployeeSummaries => Set<EmployeeSummary>();
public DbSet<Asset> Assets => Set<Asset>();
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<EmployeeSummary>()
.HasNoKey()
.ToView("EmployeeSummary");
// Table-per-hierarchy: the derived types share the base table and are told apart by a
// discriminator, which is what OfType narrows on.
builder.Entity<Vehicle>();
builder.Entity<Building>();
}
// end-snippet
protected override void ConfigureConventions(ModelConfigurationBuilder builder) =>
builder.Properties<decimal>().HavePrecision(18, 2);
public static void Initialize(SampleContext context)
{
context.Database.EnsureCreated();
if (!context.Employees.Any())
{
Seed(context);
}
// EnsureCreated does not manage views; create it once the tables and data exist.
context.Database.ExecuteSqlRaw(
"""
CREATE OR ALTER VIEW EmployeeSummary AS
SELECT d.Name AS Department, COUNT(e.Id) AS Headcount
FROM Departments d
LEFT JOIN Employees e ON e.DepartmentId = d.Id
GROUP BY d.Name;
""");
}
static void Seed(SampleContext context)
{
var engineering = new Department
{
Name = "Engineering",
// A PNG signature stands in for a real logo — enough bytes to travel as its own multipart
// part. Sales keeps a null one, so a query over both carries a diverted value and an
// inline null side by side.
Logo = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A],
// Never travels with a row at all — fetched by this row's key, and only when asked for.
// Sales keeps a null one here too: a row that exists holding no value is a distinct answer
// from a row that may not be read.
Handbook = "Engineering handbook."u8.ToArray()
};
var sales = new Department
{
Name = "Sales"
};
context.Departments.AddRange(engineering, sales);
var alice = new Employee
{
Name = "Alice",
Status = Status.FullTime,
Active = true,
Created = new(2026, 1, 4),
Department = engineering,
Salary = 200_000,
// Never travels with a row at all — fetched by this row's key, and only when something
// wants to draw it.
Photo = CartoonFace.For("Alice")
};
context.Employees.Add(alice);
context.Employees.AddRange(
new()
{
Name = "Aaron",
Status = Status.FullTime,
Active = true,
Created = new(2026, 1, 4),
Department = engineering,
Manager = alice,
Salary = 150_000,
Photo = CartoonFace.For("Aaron")
},
new()
{
Name = "Bob",
Status = Status.PartTime,
Active = false,
Created = new(2026, 2, 1),
Department = sales,
Manager = alice,
Salary = 90_000,
Photo = CartoonFace.For("Bob")
},
new()
{
Name = "Carol",
Status = Status.Contractor,
Active = true,
Created = new(2026, 3, 1),
Department = sales,
// Carol keeps a null one: a row that exists holding no value is a distinct answer
// from a row that may not be read, and the sample page shows both.
Salary = 120_000
});
context.Assets.AddRange(
new Vehicle
{
Name = "Van",
Wheels = 4
},
new Vehicle
{
Name = "Trailer",
Wheels = 2
},
new Building
{
Name = "Depot",
Floors = 3
});
context.Orders.AddRange(
new()
{
Region = "North",
Amount = 100m,
Revision = 1,
Tags = ["urgent", "export"]
},
new()
{
Region = "North",
Amount = 250m,
Revision = 2,
Tags = ["export"]
},
new()
{
Region = "South",
Amount = 75m,
Revision = 3
});
context.SaveChanges();
}
}