-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSnippets.cs
More file actions
273 lines (213 loc) · 6.7 KB
/
Copy pathSnippets.cs
File metadata and controls
273 lines (213 loc) · 6.7 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// ReSharper disable All
#pragma warning disable IDE0022
namespace Snippets;
public class EnableInterceptorExample : DbContext
{
#region EnableInterceptor
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder.UseDefaultOrderBy();
#endregion
}
public class ConfigureOrderingExample : DbContext
{
#region ConfigureOrdering
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Employee>()
.OrderBy(_ => _.HireDate)
.ThenByDescending(_ => _.Salary);
builder.Entity<Department>()
.OrderBy(_ => _.DisplayOrder);
}
#endregion
}
public class JsonColumnExample : DbContext
{
#region JsonColumnOrdering
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Order>()
.OwnsOne(_ => _.Metadata, _ => _.ToJson());
// Orders by a property of the JSON document rather than a column
builder.Entity<Order>()
.OrderByDescending(_ => _.Metadata.Priority)
.ThenBy(_ => _.Reference);
}
#endregion
}
public class RequireOrderingExample : DbContext
{
#region RequireOrdering
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder.UseDefaultOrderBy(
requireOrderingForAllEntities: true);
#endregion
}
public class ThrowOnRedundantOrderByExample : DbContext
{
#region ThrowOnRedundantOrderBy
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder.UseDefaultOrderBy(
throwOnRedundantOrderBy: true);
#endregion
}
public class OptOutOfThrowOnRedundantOrderByExample : DbContext
{
#region OptOutOfThrowOnRedundantOrderBy
// Passing false explicitly overrides OrderBySettings.ThrowOnRedundantOrderBy
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder.UseDefaultOrderBy(
throwOnRedundantOrderBy: false);
#endregion
}
public class DisableIndexCreationExample : DbContext
{
#region DisableIndexCreation
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder.UseDefaultOrderBy(
createIndexes: false);
#endregion
}
public class SnippetExamples
{
static async Task QueryWithoutOrderBy()
{
AppDbContext context = null!;
#region QueryWithoutOrderBy
// Automatically ordered by HireDate, then Salary descending
var employees = await context.Employees
.ToListAsync();
// Explicit ordering takes precedence
var employeesByName = await context.Employees
.OrderBy(_ => _.Name)
.ToListAsync();
#endregion
}
static async Task PagingAndSingleResults()
{
AppDbContext context = null!;
#region PagingAndSingleResults
// The ordering is applied before the page is taken, so the page is
// taken from an ordered sequence rather than sorted after the fact
var secondPage = await context.Employees
.Skip(20)
.Take(20)
.ToListAsync();
// Ordered by HireDate, then Salary descending, so this is the
// earliest hire rather than an arbitrary row
var first = await context.Employees
.FirstAsync();
#endregion
}
static async Task IncludeSupport()
{
AppDbContext context = null!;
#region IncludeSupport
// Departments ordered by DisplayOrder
// Employees ordered by HireDate, then Salary descending
var departments = await context.Departments
.Include(_ => _.Employees)
.ToListAsync();
#endregion
}
static void MultiColumnOrdering(ModelBuilder builder)
{
#region MultiColumnOrdering
builder.Entity<Product>()
.OrderBy(_ => _.Category)
.ThenBy(_ => _.Name)
.ThenByDescending(_ => _.Price);
#endregion
}
}
#region CompleteExample
public class Department
{
public int Id { get; set; }
public string Name { get; set; } = "";
public int DisplayOrder { get; set; }
public List<Employee> Employees { get; set; } = [];
}
public class Employee
{
public int Id { get; set; }
public int DepartmentId { get; set; }
public Department Department { get; set; } = null!;
public string Name { get; set; } = "";
public DateTime HireDate { get; set; }
public int Salary { get; set; }
}
public class AppDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
builder
.UseSqlServer("connection-string")
.UseDefaultOrderBy();
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Department>()
.OrderBy(_ => _.DisplayOrder);
builder.Entity<Employee>()
.OrderBy(_ => _.HireDate)
.ThenByDescending(_ => _.Salary);
}
public DbSet<Department> Departments => Set<Department>();
public DbSet<Employee> Employees => Set<Employee>();
}
#endregion
class Product
{
public string Category { get; set; } = "";
public string Name { get; set; } = "";
public decimal Price { get; set; }
}
public class Order
{
public int Id { get; set; }
public string Reference { get; set; } = "";
public OrderMetadata Metadata { get; set; } = new();
}
public class OrderMetadata
{
public int Priority { get; set; }
}
#region InheritanceOrdering
public class BaseEntity
{
public int Id { get; set; }
public string Name { get; set; } = "";
public int SortOrder { get; set; }
}
public class DerivedEntityA : BaseEntity
{
public string ExtraA { get; set; } = "";
}
public class DerivedEntityB : BaseEntity
{
public string ExtraB { get; set; } = "";
}
public class InheritanceDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
builder
.UseSqlServer("connection-string")
.UseDefaultOrderBy();
}
protected override void OnModelCreating(ModelBuilder builder)
{
// Configure ordering on the base entity
// DerivedEntityA and DerivedEntityB automatically inherit this ordering
builder.Entity<BaseEntity>()
.OrderBy(_ => _.SortOrder);
// Optionally, a derived type can override with its own ordering
builder.Entity<DerivedEntityB>()
.OrderByDescending(_ => _.Name);
}
public DbSet<BaseEntity> BaseEntities => Set<BaseEntity>();
public DbSet<DerivedEntityA> DerivedEntitiesA => Set<DerivedEntityA>();
public DbSet<DerivedEntityB> DerivedEntitiesB => Set<DerivedEntityB>();
}
#endregion