Repository navigation
Expand file tree
/
Copy pathAntiPatternTests.cs
More file actions
2019 lines (1760 loc) · 57.5 KB
/
Copy pathAntiPatternTests.cs
File metadata and controls
2019 lines (1760 loc) · 57.5 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
public class AntiPatternTests
{
// ReSharper disable once UnusedVariable
// ReSharper disable once UnusedParameter.Local
static void Build(string databaseName)
{
#region ThrowOnAntiPatterns
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseSqlServer(connectionString);
builder.ThrowOnAntiPatterns();
var data = new SampleDbContext(builder.Options);
#endregion
}
// ToQueryString does not connect
static string connectionString = "Server=.;Database=AntiPatterns;Trusted_Connection=True";
[Test]
public async Task IncludeThenProjection()
{
await using var data = BuildData();
#region IgnoredInclude
await ThrowsTask(() =>
data.Companies
.Include(_ => _.Employees)
.Select(_ => new
{
_.Name,
EmployeeCount = _.Employees.Count
})
.ToListAsync())
.IgnoreStackTrace();
#endregion
}
[Test]
public async Task IncludeThenScalarProjection()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.Include(_ => _.Employees)
.Select(_ => _.Name)
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task IncludeThenCount()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.Include(_ => _.Employees)
.CountAsync())
.IgnoreStackTrace();
}
[Test]
public async Task IncludeThenAny()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.Include(_ => _.Employees)
.AnyAsync(_ => _.Name == "Title"))
.IgnoreStackTrace();
}
[Test]
public async Task ThenIncludeThenProjection()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Employees
.Include(_ => _.Company)
.ThenInclude(_ => _.Employees)
.Where(_ => _.Age > 30)
.OrderBy(_ => _.Name)
.Select(_ => new
{
_.Name,
Company = _.Company.Name
})
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task StringInclude()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.Include("Employees")
.Select(_ => _.Id)
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task IncludeThenGroupByCount()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Employees
.Include(_ => _.Company)
.GroupBy(_ => _.CompanyId)
.Select(_ => _.Count())
.ToListAsync())
.IgnoreStackTrace();
}
// an entity kept by a projection is lost by a later one
[Test]
public async Task IncludeThenEntityThenScalar()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.Include(_ => _.Employees)
.Select(_ => new
{
Company = _,
_.Name
})
.Where(_ => _.Name != "")
.Select(_ => _.Company.Id)
.ToListAsync())
.IgnoreStackTrace();
}
// the query is checked by any provider, including when converted to SQL, as done when verifying a queryable
[Test]
public async Task SqlServerToQueryString()
{
await using var data = BuildSqlServerData();
var query = data.Companies
.Include(_ => _.Employees)
.Select(_ => _.Name);
var exception = Assert.ThrowsExactly<Exception>(() => query.ToQueryString());
await Assert.That(exception.Message).StartsWith("Include(_ => _.Employees) is ignored");
}
// a query that throws is not cached, so it throws every time
[Test]
public async Task ThrowsOnEachExecution()
{
await using var data = BuildData();
for (var i = 0; i < 2; i++)
{
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Companies
.Include(_ => _.Employees)
.Select(_ => _.Name)
.ToListAsync());
}
}
[Test]
public async Task IncludeEntities()
{
await using var data = BuildData();
await data.Companies
.Include(_ => _.Employees)
.Where(_ => _.Name != "")
.OrderBy(_ => _.Name)
.ToListAsync();
await data.Companies
.Include(_ => _.Employees)
.OrderBy(_ => _.Id)
.FirstOrDefaultAsync();
}
[Test]
public async Task IncludeThenProjectionOfEntity()
{
await using var data = BuildData();
await data.Companies
.Include(_ => _.Employees)
.Select(_ => new
{
Company = _,
_.Name
})
.Where(_ => _.Name != "")
.ToListAsync();
await data.Companies
.Include(_ => _.Employees)
.Select(_ => new
{
_.Name,
Employees = _.Employees.ToList()
})
.ToListAsync();
await data.Companies
.Include(_ => _.Employees)
.Select(_ => (object) _)
.ToListAsync();
}
[Test]
public async Task IncludeThenGroupByEntities()
{
await using var data = BuildData();
await data.Employees
.Include(_ => _.Company)
.GroupBy(_ => _.CompanyId)
.Select(_ => new
{
_.Key,
Employees = _.ToList()
})
.ToListAsync();
}
[Test]
public async Task ProjectionWithoutInclude()
{
await using var data = BuildData();
await data.Companies
.Select(_ => new
{
_.Name,
EmployeeCount = _.Employees.Count
})
.ToListAsync();
await data.Companies.CountAsync();
}
// Regression test: projecting into a constructor where a value is implicitly
// converted to a wider/nullable parameter type (here int -> int?) must not
// throw. EntityFinder looks past such conversions to check whether what is
// underneath is an entity, but was discarding the conversion from the
// expression it then returned. Rebuilding the containing NewExpression from
// that returned argument threw ArgumentException, even though this
// projection contains no Include or tracking option for the detector to
// legitimately flag
[Test]
public async Task ProjectionIntoConstructorWithNullableParameter()
{
await using var data = BuildData();
await data.Employees
.Select(_ => new EmployeeSummary(_.Name, _.Age))
.ToListAsync();
}
[Test]
public async Task NotEnabled()
{
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(nameof(NotEnabled));
builder.EnableServiceProviderCaching(false);
await using var data = new SampleDbContext(builder.Options);
await data.Companies
.Include(_ => _.Employees)
.Select(_ => _.Name)
.ToListAsync();
}
[Test]
public async Task AsNoTrackingThenProjection()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.AsNoTracking()
.Select(_ => _.Name)
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task ProjectionThenAsNoTracking()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.Select(_ => new
{
_.Name
})
.AsNoTracking()
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task IncludeAndAsNoTrackingThenProjection()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.Include(_ => _.Employees)
.AsNoTracking()
.Select(_ => _.Name)
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task AsTrackingThenCount()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.AsTracking()
.CountAsync())
.IgnoreStackTrace();
}
[Test]
public async Task AsNoTrackingEntities()
{
await using var data = BuildData();
await data.Companies
.AsNoTracking()
.Where(_ => _.Name != "")
.ToListAsync();
await data.Companies
.AsNoTrackingWithIdentityResolution()
.Select(_ => new
{
Company = _,
_.Name
})
.ToListAsync();
}
// a navigation is an entity, so projecting one returns entities that EF tracks
[Test]
public async Task AsNoTrackingThenProjectionOfNavigation()
{
await using var data = BuildData();
await data.Employees
.AsNoTracking()
.Select(_ => _.Company)
.ToListAsync();
await data.Companies
.AsNoTracking()
.Select(_ => _.Employees)
.ToListAsync();
await data.Employees
.AsNoTracking()
.Select(_ => new
{
_.Id,
_.Company
})
.ToListAsync();
await data.Employees
.Select(_ => _.Company)
.AsNoTracking()
.ToListAsync();
}
// a new instance of an entity type, created by the projection, is not tracked or included into
[Test]
public async Task IncludeThenProjectionIntoEntityType()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.Include(_ => _.Employees)
.AsNoTracking()
.Select(_ => new Company
{
Id = _.Id,
Name = _.Name
})
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task ProjectionIntoEntityTypeThenAsNoTracking()
{
await using var data = BuildData();
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Companies
.Select(_ => new Company
{
Id = _.Id,
Name = _.Name
})
.AsNoTracking()
.ToListAsync());
}
// the Employees in the new Company are entities, which AsNoTracking applies to
[Test]
public async Task ProjectionIntoEntityTypeWithEntitiesKept()
{
await using var data = BuildData();
await data.Companies
.AsNoTracking()
.Select(_ => new Company
{
Id = _.Id,
Name = _.Name,
Employees = _.Employees
})
.ToListAsync();
}
// only a member of the navigation is projected, so no entity is returned
[Test]
public async Task AsNoTrackingThenProjectionOfNavigationMember()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Employees
.AsNoTracking()
.Select(_ => new
{
_.Id,
CompanyName = _.Company.Name
})
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task OrderByThenOrderBy()
{
await using var data = BuildData();
#region DiscardedOrderBy
await ThrowsTask(() =>
data.Companies
.OrderBy(_ => _.Name)
.OrderBy(_ => _.Id)
.ToListAsync())
.IgnoreStackTrace();
#endregion
}
[Test]
public async Task OrderByDescendingThenOrderByDescending()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.OrderByDescending(_ => _.Name)
.OrderByDescending(_ => _.Id)
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task ThenByThenOrderBy()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.OrderBy(_ => _.Name)
.ThenBy(_ => _.Id)
.Where(_ => _.Name != "")
.OrderBy(_ => _.Id)
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task OrderByThenOrderByInProjection()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.Select(_ => new
{
_.Name,
Employees = _.Employees
.OrderBy(_ => _.Name)
.OrderBy(_ => _.Age)
.Select(_ => _.Name)
.ToList()
})
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task OrderingKept()
{
await using var data = BuildData();
await data.Companies
.OrderBy(_ => _.Name)
.ThenByDescending(_ => _.Id)
.ToListAsync();
// the second OrderBy orders the page selected by the first
await data.Companies
.OrderBy(_ => _.Name)
.Take(10)
.OrderBy(_ => _.Id)
.ToListAsync();
}
[Test]
public async Task OrderByConstant()
{
await using var data = BuildData();
#region OrderByConstant
await ThrowsTask(() =>
data.Companies
.OrderBy(_ => 1)
.Take(10)
.ToListAsync())
.IgnoreStackTrace();
#endregion
}
[Test]
public async Task OrderByVariable()
{
await using var data = BuildData();
var name = "Company1";
await ThrowsTask(() =>
data.Companies
.OrderBy(_ => name)
.Take(10)
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task ThenByConstant()
{
await using var data = BuildData();
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Companies
.OrderBy(_ => _.Name)
.ThenByDescending(_ => 0)
.ToListAsync());
}
[Test]
public async Task OrderByRowValueKept()
{
await using var data = BuildData();
await data.Companies
.OrderBy(_ => _.Name.Length * 2)
.ThenBy(_ => _.Employees.Count())
.ToListAsync();
await data.Employees
.Select(_ => _.Age)
.Order()
.ToListAsync();
}
// an anti-pattern that EF detects, but only logs. The relational pipeline logs it, so InMemory does not.
[Test]
public async Task TakeWithoutOrderBy()
{
await using var data = BuildSqlServerData();
await Throws(() =>
data.Companies
.Take(10)
.ToQueryString())
.IgnoreStackTrace();
}
// the Company of each Employee is already populated by fix-up
[Test]
public async Task IncludeInverseNavigation()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.Include(_ => _.Employees)
.ThenInclude(_ => _.Company)
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task AllowWarning()
{
#region AllowAntiPatternWarning
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseSqlServer(connectionString);
builder.ThrowOnAntiPatterns();
builder.ConfigureWarnings(_ =>
_.Ignore(CoreEventId.RowLimitingOperationWithoutOrderByWarning));
#endregion
builder.EnableServiceProviderCaching(false);
await using var data = new SampleDbContext(builder.Options);
data.Companies
.Take(10)
.ToQueryString();
}
[Test]
public async Task EnabledByEnableRecording()
{
#region EnableRecordingThrowOnAntiPatterns
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(nameof(EnabledByEnableRecording));
builder.EnableRecording(throwOnAntiPatterns: true);
#endregion
builder.EnableServiceProviderCaching(false);
await using var data = new SampleDbContext(builder.Options);
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Companies
.Include(_ => _.Employees)
.Select(_ => _.Name)
.ToListAsync());
}
// the checks are opt in, so EnableRecording alone does not apply them
[Test]
public async Task NotEnabledByEnableRecording()
{
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(nameof(NotEnabledByEnableRecording));
builder.EnableRecording();
builder.EnableServiceProviderCaching(false);
await using var data = new SampleDbContext(builder.Options);
await data.Companies
.Include(_ => _.Employees)
.Select(_ => _.Name)
.ToListAsync();
}
// a configure action selects a subset: only the checks it switches on run
[Test]
public async Task Subset()
{
#region ThrowOnAntiPatternsSubset
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(nameof(Subset));
builder.ThrowOnAntiPatterns(
_ =>
{
_.ThrowOnDiscardedOrderBy = true;
_.ThrowOnCountComparison = true;
});
#endregion
builder.EnableServiceProviderCaching(false);
await using var data = new SampleDbContext(builder.Options);
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Companies
.OrderBy(_ => _.Name)
.OrderBy(_ => _.Id)
.ToListAsync());
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Companies
.Where(_ => _.Employees.Count() > 0)
.ToListAsync());
// not selected
await data.Companies
.Include(_ => _.Employees)
.Select(_ => _.Name)
.ToListAsync();
// an EF warning, which is not selected either
await data.Companies
.Take(10)
.ToListAsync();
}
// EnableStandard in a configure action adds to the standard set
[Test]
public async Task StandardAndMore()
{
#region ThrowOnAntiPatternsStandardAndMore
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(nameof(StandardAndMore));
builder.ThrowOnAntiPatterns(
_ =>
{
_.EnableStandard();
_.ThrowOnColumnCaseConversion = true;
});
#endregion
builder.EnableServiceProviderCaching(false);
await using var data = new SampleDbContext(builder.Options);
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Companies
.Include(_ => _.Employees)
.Select(_ => _.Name)
.ToListAsync());
#pragma warning disable CA1862
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Companies
.Where(_ => _.Name.ToLower() == "company1")
.ToListAsync());
#pragma warning restore CA1862
}
// a later call adds to the checks an earlier call selected
[Test]
public async Task CallsAccumulate()
{
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(nameof(CallsAccumulate));
builder.ThrowOnAntiPatterns(_ => _.ThrowOnDiscardedOrderBy = true);
builder.ThrowOnAntiPatterns(_ => _.ThrowOnCountComparison = true);
builder.EnableServiceProviderCaching(false);
await using var data = new SampleDbContext(builder.Options);
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Companies
.OrderBy(_ => _.Name)
.OrderBy(_ => _.Id)
.ToListAsync());
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Companies
.Where(_ => _.Employees.Count() > 0)
.ToListAsync());
}
[Test]
public async Task SplitQueryWithoutCollection()
{
await using var data = BuildSqlServerData();
#region IgnoredSplitQuery
await Throws(() =>
data.Employees
.Include(_ => _.Company)
.AsSplitQuery()
.ToQueryString())
.IgnoreStackTrace();
#endregion
}
[Test]
public async Task SingleQueryWithoutCollection()
{
await using var data = BuildSqlServerData();
await Throws(() =>
data.Companies
.AsSingleQuery()
.Where(_ => _.Name != "")
.ToQueryString())
.IgnoreStackTrace();
}
[Test]
public void SplitQueryKept()
{
using var data = BuildSqlServerData();
// a single collection is split from its parent
data.Companies
.Include(_ => _.Employees)
.AsSplitQuery()
.ToQueryString();
data.Employees
.Include(_ => _.Company)
.ThenInclude(_ => _.Employees)
.AsSplitQuery()
.ToQueryString();
data.Companies
.AsSplitQuery()
.Select(_ => new
{
_.Name,
Employees = _.Employees.ToList()
})
.ToQueryString();
data.Companies
.Include("Employees")
.AsSplitQuery()
.ToQueryString();
}
// a model anti-pattern, logged when the model is built
[Test]
public async Task BoolWithDefault()
{
var builder = new DbContextOptionsBuilder<BoolWithDefaultContext>();
builder.UseSqlServer(connectionString);
builder.ThrowOnAntiPatterns();
builder.EnableServiceProviderCaching(false);
await using var data = new BoolWithDefaultContext(builder.Options);
await Throws(() => data.Model)
.IgnoreStackTrace();
}
class BoolWithDefaultContext(DbContextOptions options) :
DbContext(options)
{
protected override void OnModelCreating(ModelBuilder model) =>
model
.Entity<Flagged>()
.Property(_ => _.Enabled)
.HasDefaultValueSql("1");
}
class Flagged
{
public int Id { get; set; }
public bool Enabled { get; set; }
}
[Test]
public async Task KeyWithDefault()
{
var builder = new DbContextOptionsBuilder<KeyWithDefaultContext>();
builder.UseSqlServer(connectionString);
builder.ThrowOnAntiPatterns();
builder.EnableServiceProviderCaching(false);
await using var data = new KeyWithDefaultContext(builder.Options);
await Throws(() => data.Model)
.IgnoreStackTrace();
}
class KeyWithDefaultContext(DbContextOptions options) :
DbContext(options)
{
protected override void OnModelCreating(ModelBuilder model) =>
model
.Entity<Keyed>()
.Property(_ => _.Id)
.HasDefaultValue(1);
}
class Keyed
{
public int Id { get; set; }
}
[Test]
public async Task OptionalDependentWithoutIdentifyingProperty()
{
var builder = new DbContextOptionsBuilder<OptionalDependentContext>();
builder.UseSqlServer(connectionString);
builder.ThrowOnAntiPatterns();
builder.EnableServiceProviderCaching(false);
await using var data = new OptionalDependentContext(builder.Options);
await Throws(() => data.Model)
.IgnoreStackTrace();
}
class OptionalDependentContext(DbContextOptions options) :
DbContext(options)
{
protected override void OnModelCreating(ModelBuilder model) =>
model
.Entity<Person>()
.OwnsOne(_ => _.Address);
}
class Person
{
public int Id { get; set; }
public Address? Address { get; set; }
}
class Address
{
public string? City { get; set; }
}
[Test]
public async Task OrderByThenCount()
{
await using var data = BuildData();
#region OrderByThenCount
await ThrowsTask(() =>
data.Companies
.OrderBy(_ => _.Name)
.CountAsync())
.IgnoreStackTrace();
#endregion
}
[Test]
public async Task OrderByThenWhereThenAny()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.OrderBy(_ => _.Name)
.Where(_ => _.Name != "")
.AnyAsync())
.IgnoreStackTrace();
}
[Test]
public async Task OrderByThenCountInProjection()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.Select(_ => new
{
_.Name,
Count = _.Employees
.OrderBy(_ => _.Age)
.Count()
})
.ToListAsync())
.IgnoreStackTrace();
}
// the ordering selects which rows Take counts
[Test]
public async Task OrderByThenTakeThenCount()
{
await using var data = BuildData();
await data.Companies
.OrderBy(_ => _.Name)
.Take(10)
.CountAsync();
}
[Test]
public async Task OrderByThenSingle()
{
await using var data = BuildData();
await ThrowsTask(() =>
data.Companies
.OrderBy(_ => _.Name)
.SingleOrDefaultAsync(_ => _.Id == 1))
.IgnoreStackTrace();
}
[Test]
public async Task OrderByThenSingleInProjection()
{
await using var data = BuildData();
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Companies
.Select(_ => _.Employees
.OrderBy(_ => _.Age)
.Select(_ => _.Name)
.Single())
.ToListAsync());
}
[Test]
public async Task OrderByThenGroupBy()
{
await using var data = BuildData();
#region OrderByThenGroupBy
await ThrowsTask(() =>
data.Employees
.OrderBy(_ => _.Age)
.GroupBy(_ => _.CompanyId)
.Select(_ => _.First())
.ToListAsync())
.IgnoreStackTrace();
#endregion
}
[Test]
public async Task OrderByThenGroupByCount()
{
await using var data = BuildData();
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Employees
.OrderBy(_ => _.Name)
.GroupBy(_ => _.CompanyId, (key, employees) => new
{
key,