/
QueryBugsTest.cs
9700 lines (8089 loc) · 334 KB
/
QueryBugsTest.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
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using NetTopologySuite.Geometries;
using Newtonsoft.Json.Linq;
using Xunit;
using Xunit.Abstractions;
// ReSharper disable InconsistentNaming
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable AccessToDisposedClosure
// ReSharper disable ReturnValueOfPureMethodIsNotUsed
// ReSharper disable UnusedAutoPropertyAccessor.Local
// ReSharper disable UnusedMember.Local
namespace Microsoft.EntityFrameworkCore.Query
{
public class QueryBugsTest : IClassFixture<SqlServerFixture>
{
// ReSharper disable once UnusedParameter.Local
#pragma warning disable IDE0060 // Remove unused parameter
public QueryBugsTest(SqlServerFixture fixture, ITestOutputHelper testOutputHelper)
#pragma warning restore IDE0060 // Remove unused parameter
{
Fixture = fixture;
Fixture.TestSqlLoggerFactory.Clear();
//Fixture.TestSqlLoggerFactory.SetTestOutputHelper(testOutputHelper);
}
protected SqlServerFixture Fixture { get; }
[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public async Task Where_equals_DateTime_Now(bool async)
{
using (CreateDateTimeStore())
{
Fixture.TestSqlLoggerFactory.Clear();
using var context = new DateTimeContext(_options);
var query = context.Dates.Where(
d => d.DateTime2_2 == DateTime.Now
|| d.DateTime2_7 == DateTime.Now
|| d.DateTime == DateTime.Now
|| d.SmallDateTime == DateTime.Now);
var results = async
? await query.ToListAsync()
: query.ToList();
Assert.Empty(results);
AssertSql(
@"SELECT [d].[Id], [d].[DateTime], [d].[DateTime2], [d].[DateTime2_0], [d].[DateTime2_1], [d].[DateTime2_2], [d].[DateTime2_3], [d].[DateTime2_4], [d].[DateTime2_5], [d].[DateTime2_6], [d].[DateTime2_7], [d].[SmallDateTime]
FROM [Dates] AS [d]
WHERE ((([d].[DateTime2_2] = GETDATE()) OR ([d].[DateTime2_7] = GETDATE())) OR ([d].[DateTime] = GETDATE())) OR ([d].[SmallDateTime] = GETDATE())");
}
}
[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public async Task Where_not_equals_DateTime_Now(bool async)
{
using (CreateDateTimeStore())
{
Fixture.TestSqlLoggerFactory.Clear();
using var context = new DateTimeContext(_options);
var query = context.Dates.Where(
d => d.DateTime2_2 != DateTime.Now
&& d.DateTime2_7 != DateTime.Now
&& d.DateTime != DateTime.Now
&& d.SmallDateTime != DateTime.Now);
var results = async
? await query.ToListAsync()
: query.ToList();
Assert.Single(results);
AssertSql(
@"SELECT [d].[Id], [d].[DateTime], [d].[DateTime2], [d].[DateTime2_0], [d].[DateTime2_1], [d].[DateTime2_2], [d].[DateTime2_3], [d].[DateTime2_4], [d].[DateTime2_5], [d].[DateTime2_6], [d].[DateTime2_7], [d].[SmallDateTime]
FROM [Dates] AS [d]
WHERE ((([d].[DateTime2_2] <> GETDATE()) AND ([d].[DateTime2_7] <> GETDATE())) AND ([d].[DateTime] <> GETDATE())) AND ([d].[SmallDateTime] <> GETDATE())");
}
}
[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public async Task Where_equals_new_DateTime(bool async)
{
using (CreateDateTimeStore())
{
Fixture.TestSqlLoggerFactory.Clear();
using var context = new DateTimeContext(_options);
var query = context.Dates.Where(
d => d.SmallDateTime == new DateTime(1970, 9, 3, 12, 0, 0)
&& d.DateTime == new DateTime(1971, 9, 3, 12, 0, 10, 220)
&& d.DateTime2 == new DateTime(1972, 9, 3, 12, 0, 10, 333)
&& d.DateTime2_0 == new DateTime(1973, 9, 3, 12, 0, 10)
&& d.DateTime2_1 == new DateTime(1974, 9, 3, 12, 0, 10, 500)
&& d.DateTime2_2 == new DateTime(1975, 9, 3, 12, 0, 10, 660)
&& d.DateTime2_3 == new DateTime(1976, 9, 3, 12, 0, 10, 777)
&& d.DateTime2_4 == new DateTime(1977, 9, 3, 12, 0, 10, 888)
&& d.DateTime2_5 == new DateTime(1978, 9, 3, 12, 0, 10, 999)
&& d.DateTime2_6 == new DateTime(1979, 9, 3, 12, 0, 10, 111)
&& d.DateTime2_7 == new DateTime(1980, 9, 3, 12, 0, 10, 222));
var results = async
? await query.ToListAsync()
: query.ToList();
Assert.Single(results);
AssertSql(
@"SELECT [d].[Id], [d].[DateTime], [d].[DateTime2], [d].[DateTime2_0], [d].[DateTime2_1], [d].[DateTime2_2], [d].[DateTime2_3], [d].[DateTime2_4], [d].[DateTime2_5], [d].[DateTime2_6], [d].[DateTime2_7], [d].[SmallDateTime]
FROM [Dates] AS [d]
WHERE (((((((((([d].[SmallDateTime] = '1970-09-03T12:00:00') AND ([d].[DateTime] = '1971-09-03T12:00:10.220')) AND ([d].[DateTime2] = '1972-09-03T12:00:10.3330000')) AND ([d].[DateTime2_0] = '1973-09-03T12:00:10')) AND ([d].[DateTime2_1] = '1974-09-03T12:00:10.5')) AND ([d].[DateTime2_2] = '1975-09-03T12:00:10.66')) AND ([d].[DateTime2_3] = '1976-09-03T12:00:10.777')) AND ([d].[DateTime2_4] = '1977-09-03T12:00:10.8880')) AND ([d].[DateTime2_5] = '1978-09-03T12:00:10.99900')) AND ([d].[DateTime2_6] = '1979-09-03T12:00:10.111000')) AND ([d].[DateTime2_7] = '1980-09-03T12:00:10.2220000')");
}
}
[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public async Task Where_contains_DateTime_literals(bool async)
{
var dateTimes = new[]
{
new DateTime(1970, 9, 3, 12, 0, 0),
new DateTime(1971, 9, 3, 12, 0, 10, 220),
new DateTime(1972, 9, 3, 12, 0, 10, 333),
new DateTime(1973, 9, 3, 12, 0, 10),
new DateTime(1974, 9, 3, 12, 0, 10, 500),
new DateTime(1975, 9, 3, 12, 0, 10, 660),
new DateTime(1976, 9, 3, 12, 0, 10, 777),
new DateTime(1977, 9, 3, 12, 0, 10, 888),
new DateTime(1978, 9, 3, 12, 0, 10, 999),
new DateTime(1979, 9, 3, 12, 0, 10, 111),
new DateTime(1980, 9, 3, 12, 0, 10, 222)
};
using (CreateDateTimeStore())
{
Fixture.TestSqlLoggerFactory.Clear();
using var context = new DateTimeContext(_options);
var query = context.Dates.Where(
d => dateTimes.Contains(d.SmallDateTime)
&& dateTimes.Contains(d.DateTime)
&& dateTimes.Contains(d.DateTime2)
&& dateTimes.Contains(d.DateTime2_0)
&& dateTimes.Contains(d.DateTime2_1)
&& dateTimes.Contains(d.DateTime2_2)
&& dateTimes.Contains(d.DateTime2_3)
&& dateTimes.Contains(d.DateTime2_4)
&& dateTimes.Contains(d.DateTime2_5)
&& dateTimes.Contains(d.DateTime2_6)
&& dateTimes.Contains(d.DateTime2_7));
var results = async
? await query.ToListAsync()
: query.ToList();
Assert.Single(results);
AssertSql(
@"SELECT [d].[Id], [d].[DateTime], [d].[DateTime2], [d].[DateTime2_0], [d].[DateTime2_1], [d].[DateTime2_2], [d].[DateTime2_3], [d].[DateTime2_4], [d].[DateTime2_5], [d].[DateTime2_6], [d].[DateTime2_7], [d].[SmallDateTime]
FROM [Dates] AS [d]
WHERE ((((((((([d].[SmallDateTime] IN ('1970-09-03T12:00:00', '1971-09-03T12:00:10', '1972-09-03T12:00:10', '1973-09-03T12:00:10', '1974-09-03T12:00:10', '1975-09-03T12:00:10', '1976-09-03T12:00:10', '1977-09-03T12:00:10', '1978-09-03T12:00:10', '1979-09-03T12:00:10', '1980-09-03T12:00:10') AND [d].[DateTime] IN ('1970-09-03T12:00:00.000', '1971-09-03T12:00:10.220', '1972-09-03T12:00:10.333', '1973-09-03T12:00:10.000', '1974-09-03T12:00:10.500', '1975-09-03T12:00:10.660', '1976-09-03T12:00:10.777', '1977-09-03T12:00:10.888', '1978-09-03T12:00:10.999', '1979-09-03T12:00:10.111', '1980-09-03T12:00:10.222')) AND [d].[DateTime2] IN ('1970-09-03T12:00:00.0000000', '1971-09-03T12:00:10.2200000', '1972-09-03T12:00:10.3330000', '1973-09-03T12:00:10.0000000', '1974-09-03T12:00:10.5000000', '1975-09-03T12:00:10.6600000', '1976-09-03T12:00:10.7770000', '1977-09-03T12:00:10.8880000', '1978-09-03T12:00:10.9990000', '1979-09-03T12:00:10.1110000', '1980-09-03T12:00:10.2220000')) AND [d].[DateTime2_0] IN ('1970-09-03T12:00:00', '1971-09-03T12:00:10', '1972-09-03T12:00:10', '1973-09-03T12:00:10', '1974-09-03T12:00:10', '1975-09-03T12:00:10', '1976-09-03T12:00:10', '1977-09-03T12:00:10', '1978-09-03T12:00:10', '1979-09-03T12:00:10', '1980-09-03T12:00:10')) AND [d].[DateTime2_1] IN ('1970-09-03T12:00:00.0', '1971-09-03T12:00:10.2', '1972-09-03T12:00:10.3', '1973-09-03T12:00:10.0', '1974-09-03T12:00:10.5', '1975-09-03T12:00:10.6', '1976-09-03T12:00:10.7', '1977-09-03T12:00:10.8', '1978-09-03T12:00:10.9', '1979-09-03T12:00:10.1', '1980-09-03T12:00:10.2')) AND [d].[DateTime2_2] IN ('1970-09-03T12:00:00.00', '1971-09-03T12:00:10.22', '1972-09-03T12:00:10.33', '1973-09-03T12:00:10.00', '1974-09-03T12:00:10.50', '1975-09-03T12:00:10.66', '1976-09-03T12:00:10.77', '1977-09-03T12:00:10.88', '1978-09-03T12:00:10.99', '1979-09-03T12:00:10.11', '1980-09-03T12:00:10.22')) AND [d].[DateTime2_3] IN ('1970-09-03T12:00:00.000', '1971-09-03T12:00:10.220', '1972-09-03T12:00:10.333', '1973-09-03T12:00:10.000', '1974-09-03T12:00:10.500', '1975-09-03T12:00:10.660', '1976-09-03T12:00:10.777', '1977-09-03T12:00:10.888', '1978-09-03T12:00:10.999', '1979-09-03T12:00:10.111', '1980-09-03T12:00:10.222')) AND [d].[DateTime2_4] IN ('1970-09-03T12:00:00.0000', '1971-09-03T12:00:10.2200', '1972-09-03T12:00:10.3330', '1973-09-03T12:00:10.0000', '1974-09-03T12:00:10.5000', '1975-09-03T12:00:10.6600', '1976-09-03T12:00:10.7770', '1977-09-03T12:00:10.8880', '1978-09-03T12:00:10.9990', '1979-09-03T12:00:10.1110', '1980-09-03T12:00:10.2220')) AND [d].[DateTime2_5] IN ('1970-09-03T12:00:00.00000', '1971-09-03T12:00:10.22000', '1972-09-03T12:00:10.33300', '1973-09-03T12:00:10.00000', '1974-09-03T12:00:10.50000', '1975-09-03T12:00:10.66000', '1976-09-03T12:00:10.77700', '1977-09-03T12:00:10.88800', '1978-09-03T12:00:10.99900', '1979-09-03T12:00:10.11100', '1980-09-03T12:00:10.22200')) AND [d].[DateTime2_6] IN ('1970-09-03T12:00:00.000000', '1971-09-03T12:00:10.220000', '1972-09-03T12:00:10.333000', '1973-09-03T12:00:10.000000', '1974-09-03T12:00:10.500000', '1975-09-03T12:00:10.660000', '1976-09-03T12:00:10.777000', '1977-09-03T12:00:10.888000', '1978-09-03T12:00:10.999000', '1979-09-03T12:00:10.111000', '1980-09-03T12:00:10.222000')) AND [d].[DateTime2_7] IN ('1970-09-03T12:00:00.0000000', '1971-09-03T12:00:10.2200000', '1972-09-03T12:00:10.3330000', '1973-09-03T12:00:10.0000000', '1974-09-03T12:00:10.5000000', '1975-09-03T12:00:10.6600000', '1976-09-03T12:00:10.7770000', '1977-09-03T12:00:10.8880000', '1978-09-03T12:00:10.9990000', '1979-09-03T12:00:10.1110000', '1980-09-03T12:00:10.2220000')");
}
}
private class DatesAndPrunes
{
public int Id { get; set; }
[Column(TypeName = "smalldatetime")]
public DateTime SmallDateTime { get; set; }
[Column(TypeName = "datetime")]
public DateTime DateTime { get; set; }
[Column(TypeName = "datetime2")]
public DateTime DateTime2 { get; set; }
[Column(TypeName = "datetime2(0)")]
public DateTime DateTime2_0 { get; set; }
[Column(TypeName = "datetime2(1)")]
public DateTime DateTime2_1 { get; set; }
[Column(TypeName = "datetime2(2)")]
public DateTime DateTime2_2 { get; set; }
[Column(TypeName = "datetime2(3)")]
public DateTime DateTime2_3 { get; set; }
[Column(TypeName = "datetime2(4)")]
public DateTime DateTime2_4 { get; set; }
[Column(TypeName = "datetime2(5)")]
public DateTime DateTime2_5 { get; set; }
[Column(TypeName = "datetime2(6)")]
public DateTime DateTime2_6 { get; set; }
[Column(TypeName = "datetime2(7)")]
public DateTime DateTime2_7 { get; set; }
}
private class DateTimeContext : DbContext
{
public DateTimeContext(DbContextOptions options)
: base(options)
{
}
public DbSet<DatesAndPrunes> Dates { get; set; }
}
private SqlServerTestStore CreateDateTimeStore()
=> CreateTestStore(
() => new DateTimeContext(_options),
c =>
{
c.Add(
new DatesAndPrunes
{
SmallDateTime = new DateTime(1970, 9, 3, 12, 0, 0),
DateTime = new DateTime(1971, 9, 3, 12, 0, 10, 220),
DateTime2 = new DateTime(1972, 9, 3, 12, 0, 10, 333),
DateTime2_0 = new DateTime(1973, 9, 3, 12, 0, 10),
DateTime2_1 = new DateTime(1974, 9, 3, 12, 0, 10, 500),
DateTime2_2 = new DateTime(1975, 9, 3, 12, 0, 10, 660),
DateTime2_3 = new DateTime(1976, 9, 3, 12, 0, 10, 777),
DateTime2_4 = new DateTime(1977, 9, 3, 12, 0, 10, 888),
DateTime2_5 = new DateTime(1978, 9, 3, 12, 0, 10, 999),
DateTime2_6 = new DateTime(1979, 9, 3, 12, 0, 10, 111),
DateTime2_7 = new DateTime(1980, 9, 3, 12, 0, 10, 222)
});
c.SaveChanges();
});
#region Bug6901
[ConditionalFact]
public void Left_outer_join_bug_6091()
{
using var testStore = SqlServerTestStore.CreateInitialized("QueryBugsTest");
testStore.ExecuteNonQuery(
@"
CREATE TABLE [dbo].[Customers](
[CustomerID] [int] NOT NULL PRIMARY KEY,
[CustomerName] [varchar](120) NULL,
[PostcodeID] [int] NULL);
CREATE TABLE [dbo].[Postcodes](
[PostcodeID] [int] NOT NULL PRIMARY KEY,
[PostcodeValue] [varchar](100) NOT NULL,
[TownName] [varchar](255) NOT NULL);
INSERT [dbo].[Customers] ([CustomerID], [CustomerName], [PostcodeID]) VALUES (1, N'Sam Tippet', 5);
INSERT [dbo].[Customers] ([CustomerID], [CustomerName], [PostcodeID]) VALUES (2, N'William Greig', 2);
INSERT [dbo].[Customers] ([CustomerID], [CustomerName], [PostcodeID]) VALUES (3, N'Steve Jones', 3);
INSERT [dbo].[Customers] ([CustomerID], [CustomerName], [PostcodeID]) VALUES (4, N'Jim Warren', NULL);
INSERT [dbo].[Customers] ([CustomerID], [CustomerName], [PostcodeID]) VALUES (5, N'Andrew Smith', 5);
INSERT [dbo].[Postcodes] ([PostcodeID], [PostcodeValue], [TownName]) VALUES (2, N'1000', N'Town 1');
INSERT [dbo].[Postcodes] ([PostcodeID], [PostcodeValue], [TownName]) VALUES (3, N'2000', N'Town 2');
INSERT [dbo].[Postcodes] ([PostcodeID], [PostcodeValue], [TownName]) VALUES (4, N'3000', N'Town 3');
INSERT [dbo].[Postcodes] ([PostcodeID], [PostcodeValue], [TownName]) VALUES (5, N'4000', N'Town 4');
");
using var context = new Bug6091Context(Fixture.CreateOptions(testStore));
var customers
= from customer in context.Customers
join postcode in context.Postcodes
on customer.PostcodeID equals postcode.PostcodeID into custPCTmp
from custPC in custPCTmp.DefaultIfEmpty()
select new
{
customer.CustomerID,
customer.CustomerName,
TownName = custPC == null ? string.Empty : custPC.TownName,
PostcodeValue = custPC == null ? string.Empty : custPC.PostcodeValue
};
var results = customers.ToList();
Assert.Equal(5, results.Count);
Assert.True(results[3].CustomerName != results[4].CustomerName);
}
private class Bug6091Context : DbContext
{
public Bug6091Context(DbContextOptions options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().ToTable("Customers");
}
public DbSet<Customer> Customers { get; set; }
public DbSet<Postcode> Postcodes { get; set; }
// ReSharper disable once MemberHidesStaticFromOuterClass
public class Customer
{
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public int? PostcodeID { get; set; }
}
public class Postcode
{
public int PostcodeID { get; set; }
public string PostcodeValue { get; set; }
public string TownName { get; set; }
}
}
#endregion
#region Bug5481
[ConditionalFact]
public async Task Multiple_optional_navs_should_not_deadlock_bug_5481()
{
using var testStore = SqlServerTestStore.CreateInitialized("QueryBugsTest");
using var context = new DeadlockContext(Fixture.CreateOptions(testStore));
context.Database.EnsureCreatedResiliently();
context.EnsureSeeded();
var count
= await context.Persons
.Where(
p => p.AddressOne != null && p.AddressOne.Street.Contains("Low Street")
|| p.AddressTwo != null && p.AddressTwo.Street.Contains("Low Street"))
.CountAsync();
Assert.Equal(0, count);
}
private class DeadlockContext : DbContext
{
public DeadlockContext(DbContextOptions options)
: base(options)
{
}
public DbSet<Person> Persons { get; set; }
public DbSet<Address> Addresses { get; set; }
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public int PersonId { get; set; }
public Person Person { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int? AddressOneId { get; set; }
public Address AddressOne { get; set; }
public int? AddressTwoId { get; set; }
public Address AddressTwo { get; set; }
}
public void EnsureSeeded()
{
if (!Persons.Any())
{
AddRange(
new Person { Name = "John Doe" },
new Person { Name = "Joe Bloggs" });
SaveChanges();
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Person>().HasKey(p => p.Id);
modelBuilder.Entity<Person>().Property(p => p.Name)
.IsRequired();
modelBuilder.Entity<Person>().HasOne(p => p.AddressOne)
.WithMany()
.HasForeignKey(p => p.AddressOneId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Person>().Property(p => p.AddressOneId);
modelBuilder.Entity<Person>().HasOne(p => p.AddressTwo)
.WithMany()
.HasForeignKey(p => p.AddressTwoId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Person>().Property(p => p.AddressTwoId);
modelBuilder.Entity<Address>().HasKey(a => a.Id);
modelBuilder.Entity<Address>().Property(a => a.Street).IsRequired();
modelBuilder.Entity<Address>().HasOne(a => a.Person)
.WithMany()
.HasForeignKey(a => a.PersonId)
.OnDelete(DeleteBehavior.Restrict);
}
}
#endregion
[ConditionalFact]
public void Query_when_null_key_in_database_should_throw()
{
using var testStore = SqlServerTestStore.CreateInitialized("QueryBugsTest");
testStore.ExecuteNonQuery(
@"CREATE TABLE ZeroKey (Id int);
INSERT ZeroKey VALUES (NULL)");
var options = Fixture.CreateOptions(testStore);
options = new DbContextOptionsBuilder(options).EnableDetailedErrors().Options;
using var context = new NullKeyContext(options);
Assert.Equal(
RelationalStrings.ErrorMaterializingPropertyNullReference("ZeroKey", "Id", typeof(int)),
Assert.Throws<InvalidOperationException>(() => context.ZeroKeys.ToList()).Message);
}
private class NullKeyContext : DbContext
{
public NullKeyContext(DbContextOptions options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ZeroKey>().ToTable("ZeroKey");
}
public DbSet<ZeroKey> ZeroKeys { get; set; }
public class ZeroKey
{
public int Id { get; set; }
}
}
#region Bug603
[ConditionalFact]
public async Task First_FirstOrDefault_ix_async_bug_603()
{
using (CreateDatabase603())
{
using (var context = new MyContext603(_options))
{
context.Products.Add(
new Product { Name = "Product 1" });
context.SaveChanges();
}
using var ctx = new MyContext603(_options);
var product = await ctx.Products.OrderBy(p => p.Id).FirstAsync();
ctx.Products.Remove(product);
await ctx.SaveChangesAsync();
}
using (CreateDatabase603())
{
using (var context = new MyContext603(_options))
{
context.Products.Add(
new Product { Name = "Product 1" });
context.SaveChanges();
}
using var ctx = new MyContext603(_options);
var product = await ctx.Products.OrderBy(p => p.Id).FirstOrDefaultAsync();
ctx.Products.Remove(product);
await ctx.SaveChangesAsync();
}
}
private class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
private class MyContext603 : DbContext
{
public MyContext603(DbContextOptions options)
: base(options)
{
}
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().ToTable("Product");
}
}
private SqlServerTestStore CreateDatabase603()
{
return CreateTestStore(() => new MyContext603(_options), null);
}
#endregion
#region Bugs925_926
[ConditionalFact]
public void Include_on_entity_with_composite_key_One_To_Many_bugs_925_926()
{
using (CreateDatabase925())
{
using var ctx = new MyContext925(_options);
var query = ctx.Customers.Include(c => c.Orders).OrderBy(c => c.FirstName).ThenBy(c => c.LastName);
var result = query.ToList();
Assert.Equal(2, result.Count);
Assert.Equal(2, result[0].Orders.Count);
Assert.Equal(3, result[1].Orders.Count);
AssertSql(
@"SELECT [c].[FirstName], [c].[LastName], [o].[Id], [o].[CustomerFirstName], [o].[CustomerLastName], [o].[Name]
FROM [Customer] AS [c]
LEFT JOIN [Order] AS [o] ON ([c].[FirstName] = [o].[CustomerFirstName]) AND ([c].[LastName] = [o].[CustomerLastName])
ORDER BY [c].[FirstName], [c].[LastName], [o].[Id]");
}
}
[ConditionalFact]
public void Include_on_entity_with_composite_key_Many_To_One_bugs_925_926()
{
using (CreateDatabase925())
{
using var ctx = new MyContext925(_options);
var query = ctx.Orders.Include(o => o.Customer);
var result = query.ToList();
Assert.Equal(5, result.Count);
Assert.NotNull(result[0].Customer);
Assert.NotNull(result[1].Customer);
Assert.NotNull(result[2].Customer);
Assert.NotNull(result[3].Customer);
Assert.NotNull(result[4].Customer);
AssertSql(
@"SELECT [o].[Id], [o].[CustomerFirstName], [o].[CustomerLastName], [o].[Name], [c].[FirstName], [c].[LastName]
FROM [Order] AS [o]
LEFT JOIN [Customer] AS [c] ON ([o].[CustomerFirstName] = [c].[FirstName]) AND ([o].[CustomerLastName] = [c].[LastName])");
}
}
private SqlServerTestStore CreateDatabase925()
{
return CreateTestStore(
() => new MyContext925(_options),
context =>
{
var order11 = new Order { Name = "Order11" };
var order12 = new Order { Name = "Order12" };
var order21 = new Order { Name = "Order21" };
var order22 = new Order { Name = "Order22" };
var order23 = new Order { Name = "Order23" };
var customer1 = new Customer
{
FirstName = "Customer",
LastName = "One",
Orders = new List<Order> { order11, order12 }
};
var customer2 = new Customer
{
FirstName = "Customer",
LastName = "Two",
Orders = new List<Order>
{
order21,
order22,
order23
}
};
context.Customers.AddRange(customer1, customer2);
context.Orders.AddRange(order11, order12, order21, order22, order23);
context.SaveChanges();
ClearLog();
});
}
private class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Order> Orders { get; set; }
}
private class Order
{
public int Id { get; set; }
public string Name { get; set; }
public Customer Customer { get; set; }
}
private class MyContext925 : DbContext
{
public MyContext925(DbContextOptions options)
: base(options)
{
}
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>(
m =>
{
m.ToTable("Customer");
m.HasKey(
c => new { c.FirstName, c.LastName });
m.HasMany(c => c.Orders).WithOne(o => o.Customer);
});
modelBuilder.Entity<Order>().ToTable("Order");
}
}
#endregion
#region Bug963
[ConditionalFact]
public void Include_on_optional_navigation_One_To_Many_963()
{
using (CreateDatabase963())
{
using var ctx = new MyContext963(_options);
ctx.Targaryens.Include(t => t.Dragons).ToList();
}
}
[ConditionalFact]
public void Include_on_optional_navigation_Many_To_One_963()
{
using (CreateDatabase963())
{
using var ctx = new MyContext963(_options);
ctx.Dragons.Include(d => d.Mother).ToList();
}
}
[ConditionalFact]
public void Include_on_optional_navigation_One_To_One_principal_963()
{
using (CreateDatabase963())
{
using var ctx = new MyContext963(_options);
ctx.Targaryens.Include(t => t.Details).ToList();
}
}
[ConditionalFact]
public void Include_on_optional_navigation_One_To_One_dependent_963()
{
using (CreateDatabase963())
{
using var ctx = new MyContext963(_options);
ctx.Details.Include(d => d.Targaryen).ToList();
}
}
[ConditionalFact]
public void Join_on_optional_navigation_One_To_Many_963()
{
using (CreateDatabase963())
{
using var ctx = new MyContext963(_options);
(from t in ctx.Targaryens
join d in ctx.Dragons on t.Id equals d.MotherId
select d).ToList();
}
}
private SqlServerTestStore CreateDatabase963()
{
return CreateTestStore(
() => new MyContext963(_options),
context =>
{
var drogon = new Dragon { Name = "Drogon" };
var rhaegal = new Dragon { Name = "Rhaegal" };
var viserion = new Dragon { Name = "Viserion" };
var balerion = new Dragon { Name = "Balerion" };
var aerys = new Targaryen { Name = "Aerys II" };
var details = new Details
{
FullName = @"Daenerys Stormborn of the House Targaryen, the First of Her Name, the Unburnt, Queen of Meereen,
Queen of the Andals and the Rhoynar and the First Men, Khaleesi of the Great Grass Sea, Breaker of Chains, and Mother of Dragons"
};
var daenerys = new Targaryen
{
Name = "Daenerys",
Details = details,
Dragons = new List<Dragon>
{
drogon,
rhaegal,
viserion
}
};
context.Targaryens.AddRange(daenerys, aerys);
context.Dragons.AddRange(drogon, rhaegal, viserion, balerion);
context.Details.Add(details);
context.SaveChanges();
});
}
private class Targaryen
{
public int Id { get; set; }
public string Name { get; set; }
public Details Details { get; set; }
public List<Dragon> Dragons { get; set; }
}
private class Dragon
{
public int Id { get; set; }
public string Name { get; set; }
public int? MotherId { get; set; }
public Targaryen Mother { get; set; }
}
private class Details
{
public int Id { get; set; }
public int? TargaryenId { get; set; }
public Targaryen Targaryen { get; set; }
public string FullName { get; set; }
}
// TODO: replace with GearsOfWar context when it's refactored properly
private class MyContext963 : DbContext
{
public MyContext963(DbContextOptions options)
: base(options)
{
}
public DbSet<Targaryen> Targaryens { get; set; }
// ReSharper disable once MemberHidesStaticFromOuterClass
public DbSet<Details> Details { get; set; }
public DbSet<Dragon> Dragons { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Targaryen>(
m =>
{
m.ToTable("Targaryen");
m.HasKey(t => t.Id);
m.HasMany(t => t.Dragons).WithOne(d => d.Mother).HasForeignKey(d => d.MotherId);
m.HasOne(t => t.Details).WithOne(d => d.Targaryen).HasForeignKey<Details>(d => d.TargaryenId);
});
modelBuilder.Entity<Dragon>().ToTable("Dragon");
}
}
#endregion
#region Bug1742
[ConditionalFact]
public void Compiler_generated_local_closure_produces_valid_parameter_name_1742()
{
Execute1742(
new CustomerDetails_1742 { FirstName = "Foo", LastName = "Bar" });
}
private void Execute1742(CustomerDetails_1742 details)
{
using (CreateDatabase925())
{
using var ctx = new MyContext925(_options);
var firstName = details.FirstName;
ctx.Customers.Where(c => c.FirstName == firstName && c.LastName == details.LastName).ToList();
// No AssertSQL since compiler generated variable names are different between local and CI
}
}
private class CustomerDetails_1742
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
#endregion
#region Bug3758
[ConditionalFact]
public void Customer_collections_materialize_properly_3758()
{
using (CreateDatabase3758())
{
using var ctx = new MyContext3758(_options);
var query1 = ctx.Customers.Select(c => c.Orders1);
var result1 = query1.ToList();
Assert.Equal(2, result1.Count);
Assert.IsType<HashSet<Order3758>>(result1[0]);
Assert.Equal(2, result1[0].Count);
Assert.Equal(2, result1[1].Count);
var query2 = ctx.Customers.Select(c => c.Orders2);
var result2 = query2.ToList();
Assert.Equal(2, result2.Count);
Assert.IsType<MyGenericCollection3758<Order3758>>(result2[0]);
Assert.Equal(2, result2[0].Count);
Assert.Equal(2, result2[1].Count);
var query3 = ctx.Customers.Select(c => c.Orders3);
var result3 = query3.ToList();
Assert.Equal(2, result3.Count);
Assert.IsType<MyNonGenericCollection3758>(result3[0]);
Assert.Equal(2, result3[0].Count);
Assert.Equal(2, result3[1].Count);
var query4 = ctx.Customers.Select(c => c.Orders4);
Assert.Equal(
CoreStrings.NavigationCannotCreateType(
"Orders4", typeof(Customer3758).Name,
typeof(MyInvalidCollection3758<Order3758>).ShortDisplayName()),
Assert.Throws<InvalidOperationException>(() => query4.ToList()).Message);
}
}
private class MyContext3758 : DbContext
{
public MyContext3758(DbContextOptions options)
: base(options)
{
}
public DbSet<Customer3758> Customers { get; set; }
public DbSet<Order3758> Orders { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer3758>(
b =>
{
b.ToTable("Customer3758");
b.HasMany(e => e.Orders1).WithOne().HasForeignKey("CustomerId1");
b.HasMany(e => e.Orders2).WithOne().HasForeignKey("CustomerId2");
b.HasMany(e => e.Orders3).WithOne().HasForeignKey("CustomerId3");
b.HasMany(e => e.Orders4).WithOne().HasForeignKey("CustomerId4");
});
modelBuilder.Entity<Order3758>().ToTable("Order3758");
}
}
private class Customer3758
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Order3758> Orders1 { get; set; }
public MyGenericCollection3758<Order3758> Orders2 { get; set; }
public MyNonGenericCollection3758 Orders3 { get; set; }
public MyInvalidCollection3758<Order3758> Orders4 { get; set; }
}
private class Order3758
{
public int Id { get; set; }
public string Name { get; set; }
}
private class MyGenericCollection3758<TElement> : List<TElement>
{
}
private class MyNonGenericCollection3758 : List<Order3758>
{
}
private class MyInvalidCollection3758<TElement> : List<TElement>
{
public MyInvalidCollection3758(int argument)
{
var _ = argument;
}
}
private SqlServerTestStore CreateDatabase3758()
{
return CreateTestStore(
() => new MyContext3758(_options),
context =>
{
var o111 = new Order3758 { Name = "O111" };
var o112 = new Order3758 { Name = "O112" };
var o121 = new Order3758 { Name = "O121" };
var o122 = new Order3758 { Name = "O122" };
var o131 = new Order3758 { Name = "O131" };
var o132 = new Order3758 { Name = "O132" };
var o141 = new Order3758 { Name = "O141" };
var o211 = new Order3758 { Name = "O211" };
var o212 = new Order3758 { Name = "O212" };
var o221 = new Order3758 { Name = "O221" };
var o222 = new Order3758 { Name = "O222" };
var o231 = new Order3758 { Name = "O231" };
var o232 = new Order3758 { Name = "O232" };
var o241 = new Order3758 { Name = "O241" };
var c1 = new Customer3758
{
Name = "C1",
Orders1 = new List<Order3758> { o111, o112 },
Orders2 = new MyGenericCollection3758<Order3758>(),
Orders3 = new MyNonGenericCollection3758(),
Orders4 = new MyInvalidCollection3758<Order3758>(42)
};
c1.Orders2.AddRange(new[] { o121, o122 });
c1.Orders3.AddRange(new[] { o131, o132 });
c1.Orders4.Add(o141);
var c2 = new Customer3758
{
Name = "C2",
Orders1 = new List<Order3758> { o211, o212 },
Orders2 = new MyGenericCollection3758<Order3758>(),