-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathComplexCollectionJsonUpdateTestBase.cs
More file actions
926 lines (818 loc) · 33.7 KB
/
Copy pathComplexCollectionJsonUpdateTestBase.cs
File metadata and controls
926 lines (818 loc) · 33.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.EntityFrameworkCore.Update;
public abstract class ComplexCollectionJsonUpdateTestBase<TFixture>(TFixture fixture) : IClassFixture<TFixture>
where TFixture : ComplexCollectionJsonUpdateTestBase<TFixture>.ComplexCollectionJsonUpdateFixtureBase, new()
{
public TFixture Fixture { get; } = fixture;
protected ComplexCollectionJsonContext CreateContext()
=> (ComplexCollectionJsonContext)Fixture.CreateContext();
[ConditionalFact]
public virtual Task Add_element_to_complex_collection_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
var companyEntry = context.Entry(company);
var budgetProperty = companyEntry.ComplexProperty(c => c.Department).Property(c => c.Budget);
Assert.Equal(budgetProperty.CurrentValue, budgetProperty.OriginalValue);
company.Contacts!.Add(new Contact { Name = "New Contact", PhoneNumbers = ["555-0000"] });
Assert.Contains("Contacts (Complex: List<Contact>)", context.ChangeTracker.DebugView.LongView);
Assert.Contains("Department (Complex: Department)", context.ChangeTracker.DebugView.LongView);
Assert.Contains("Name: 'Initial Department'", context.ChangeTracker.DebugView.LongView);
Assert.Contains("Employees (Complex: List<Employee>)", context.ChangeTracker.DebugView.LongView);
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.Equal(3, company.Contacts!.Count);
Assert.Equal("New Contact", company.Contacts[2].Name);
Assert.Single(company.Contacts[2].PhoneNumbers);
Assert.Equal("555-0000", company.Contacts[2].PhoneNumbers[0]);
}
});
[ConditionalFact]
public virtual Task Remove_element_from_complex_collection_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Contacts!.RemoveAt(0);
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.Single(company.Contacts!);
Assert.Equal("Second Contact", company.Contacts![0].Name);
}
});
[ConditionalFact]
public virtual Task Delete_complex_collection_owner_entity_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.SingleAsync(c => c.Id == 1);
context.Remove(company);
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var exists = await context.Companies.AnyAsync(c => c.Id == 1);
Assert.False(exists);
}
});
[ConditionalFact]
public virtual Task Modify_element_in_complex_collection_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Contacts![0].Name = "First Contact - Modified";
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.Equal("First Contact - Modified", company.Contacts![0].Name);
}
});
[ConditionalFact]
public virtual Task Move_elements_in_complex_collection_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
var temp = company.Contacts![0];
company.Contacts[0] = company.Contacts[1];
company.Contacts[1] = temp;
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.Equal("Second Contact", company.Contacts![0].Name);
Assert.Equal("First Contact", company.Contacts[1].Name);
}
});
[ConditionalFact]
public virtual Task Change_complex_collection_mapped_to_json_to_null_and_to_empty()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
ClearLog();
company.Contacts!.Clear();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.NotNull(company.Contacts);
Assert.Empty(company.Contacts);
company.Contacts = null;
}
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.Null(company.Contacts);
}
});
[ConditionalFact]
public virtual Task Complex_collection_with_nested_complex_type_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Employees =
[
new Employee
{
Name = "John Doe",
PhoneNumbers = ["555-1234", "555-5678"],
Address = new Address
{
Street = "123 Main St",
City = "Seattle",
PostalCode = "98101",
Country = "USA"
}
},
new Employee
{
Name = "Jane Smith",
PhoneNumbers = ["555-9876"],
Address = new Address
{
Street = "456 Oak Ave",
City = "Portland",
PostalCode = "97201",
Country = "USA"
}
}
];
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.Equal(2, company.Employees!.Count);
var john = company.Employees[0];
Assert.Equal("John Doe", john.Name);
Assert.Equal("123 Main St", john.Address.Street);
Assert.Equal("Seattle", john.Address.City);
var jane = company.Employees[1];
Assert.Equal("Jane Smith", jane.Name);
Assert.Equal("456 Oak Ave", jane.Address.Street);
Assert.Equal("Portland", jane.Address.City);
}
});
[ConditionalFact]
public virtual Task Modify_multiple_complex_properties_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Contacts = [new Contact { Name = "Contact 1", PhoneNumbers = ["555-1111"] }];
company.Department = new Department { Name = "Department A", Budget = 50000.00m };
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.Single(company.Contacts!);
Assert.Equal("Contact 1", company.Contacts![0].Name);
Assert.NotNull(company.Department);
Assert.Equal("Department A", company.Department.Name);
Assert.Equal(50000.00m, company.Department.Budget);
}
});
[ConditionalFact]
public virtual Task Clear_complex_collection_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Contacts!.Clear();
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.Empty(company.Contacts!);
}
});
[ConditionalFact]
public virtual Task Replace_entire_complex_collection_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Contacts =
[
new Contact { Name = "Replacement Contact 1", PhoneNumbers = ["999-1111"] },
new Contact { Name = "Replacement Contact 2", PhoneNumbers = ["999-2222", "999-3333"] }
];
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.Equal(2, company.Contacts!.Count);
Assert.Equal("Replacement Contact 1", company.Contacts[0].Name);
Assert.Equal("Replacement Contact 2", company.Contacts[1].Name);
}
});
[ConditionalFact]
public virtual Task Add_element_to_nested_complex_collection_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Employees![0].PhoneNumbers.Add("555-9999");
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
var employee = company.Employees![0];
Assert.Equal(2, employee.PhoneNumbers.Count);
Assert.Equal("555-0001", employee.PhoneNumbers[0]);
Assert.Equal("555-9999", employee.PhoneNumbers[1]);
}
});
[ConditionalFact]
public virtual Task Modify_nested_complex_property_in_complex_collection_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Employees![0].Address.City = "Modified City";
company.Employees[0].Address.PostalCode = "99999";
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
var employee = company.Employees![0];
Assert.Equal("Modified City", employee.Address.City);
Assert.Equal("99999", employee.Address.PostalCode);
Assert.Equal("100 First St", employee.Address.Street); // Unchanged
Assert.Equal("USA", employee.Address.Country); // Unchanged
}
});
[ConditionalFact]
public virtual Task Set_complex_collection_to_null_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
var companyEntry = context.Entry(company);
var employeesProperty = companyEntry.ComplexCollection(c => c.Employees);
Assert.Equal(
employeesProperty.CurrentValue,
employeesProperty.GetInfrastructure().GetOriginalValue(employeesProperty.Metadata));
company.Employees = null;
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.Null(company.Employees);
}
});
[ConditionalFact]
public virtual Task Set_null_complex_collection_to_non_empty_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Employees = null;
await context.SaveChangesAsync();
company.Employees =
[
new Employee
{
Name = "New Employee",
PhoneNumbers = ["555-1111"],
Address = new Address
{
Street = "123 New St",
City = "New City",
PostalCode = "12345",
Country = "USA"
}
}
];
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.NotNull(company.Employees);
Assert.Single(company.Employees);
Assert.Equal("New Employee", company.Employees[0].Name);
}
});
[ConditionalFact]
public virtual Task Replace_complex_collection_element_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Employees![0] = new Employee
{
Name = "Replacement Employee",
PhoneNumbers = ["555-7777", "555-8888"],
Address = new Address
{
Street = "789 Replace St",
City = "Replace City",
PostalCode = "54321",
Country = "Canada"
}
};
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
var employee = company.Employees![0];
Assert.Equal("Replacement Employee", employee.Name);
Assert.Equal(2, employee.PhoneNumbers.Count);
Assert.Equal("555-7777", employee.PhoneNumbers[0]);
Assert.Equal("789 Replace St", employee.Address.Street);
Assert.Equal("Canada", employee.Address.Country);
}
});
[ConditionalFact]
public virtual Task Complex_collection_with_empty_nested_collections_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Employees!.Add(
new Employee
{
Name = "Employee No Phone",
PhoneNumbers = [], // Empty collection
Address = new Address
{
Street = "456 No Phone St",
City = "Quiet City",
PostalCode = "00000",
Country = "USA"
}
});
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.Equal(2, company.Employees!.Count);
var employeeWithoutPhone = company.Employees[1];
Assert.Equal("Employee No Phone", employeeWithoutPhone.Name);
Assert.Empty(employeeWithoutPhone.PhoneNumbers);
Assert.Equal("Quiet City", employeeWithoutPhone.Address.City);
}
});
[ConditionalFact]
public virtual Task Set_complex_property_mapped_to_json_to_null()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Department = null;
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.Null(company.Department);
}
});
[ConditionalFact]
public virtual Task Set_null_complex_property_to_non_null_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Department = null;
await context.SaveChangesAsync();
company.Department = new Department { Name = "New Department", Budget = 25000.00m };
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.NotNull(company.Department);
Assert.Equal("New Department", company.Department.Name);
Assert.Equal(25000.00m, company.Department.Budget);
}
});
[ConditionalFact]
public virtual Task Replace_complex_property_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
company.Department = new Department { Name = "Replacement Department", Budget = 99999.99m };
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var company = await context.Companies.OrderBy(c => c.Id).FirstAsync();
Assert.NotNull(company.Department);
Assert.Equal("Replacement Department", company.Department.Name);
Assert.Equal(99999.99m, company.Department.Budget);
}
});
[ConditionalFact]
public virtual Task Grow_nested_sub_collection_in_complex_property_mapped_to_json()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var widget = await context.Set<WidgetWithDeepJson>().OrderBy(w => w.Id).FirstAsync();
// Replace the entire JSON-mapped complex property with a structure where one nested
// sub-collection (Inner) has grown from one element to two. The sibling sub-collection
// (Others) being present in the type definition is required to trigger the regression.
widget.Deep = new DeepData
{
Mid = new MiddleData
{
Items =
[
new DeepItem
{
Title = "Item1",
Inner =
[
new InnerEntry { Value = "inner-0" },
new InnerEntry { Value = "inner-1" }
],
Others = []
}
]
}
};
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var widget = await context.Set<WidgetWithDeepJson>().OrderBy(w => w.Id).FirstAsync();
var item = Assert.Single(widget.Deep.Mid.Items);
Assert.Equal("Item1", item.Title);
Assert.Equal(2, item.Inner.Count);
Assert.Equal("inner-0", item.Inner[0].Value);
Assert.Equal("inner-1", item.Inner[1].Value);
Assert.Empty(item.Others);
}
});
[ConditionalFact]
public virtual Task Set_nullable_complex_property_with_nested_collection_to_null()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var entity = await context.Set<EntityWithNullableMeta>().OrderBy(e => e.Id).FirstAsync();
entity.Items[0].Meta = null;
ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
using (SuspendRecordingEvents())
{
var entity = await context.Set<EntityWithNullableMeta>().OrderBy(e => e.Id).FirstAsync();
Assert.Single(entity.Items);
Assert.Null(entity.Items[0].Meta);
}
});
protected virtual void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
=> facade.UseTransaction(transaction.GetDbTransaction());
protected virtual void ClearLog()
=> Fixture.TestSqlLoggerFactory.Clear();
protected virtual void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
protected virtual IDisposable SuspendRecordingEvents()
=> Fixture.TestSqlLoggerFactory.SuspendRecordingEvents();
protected class ComplexCollectionJsonContext(DbContextOptions options) : DbContext(options)
{
public DbSet<CompanyWithComplexCollections> Companies { get; set; } = null!;
public DbSet<WidgetWithDeepJson> Widgets { get; set; } = null!;
public DbSet<EntityWithNullableMeta> EntitiesWithNullableMeta { get; set; } = null!;
}
protected class CompanyWithComplexCollections
{
public int Id { get; set; }
public required string Name { get; set; }
public List<Contact>? Contacts { get; set; }
public List<Employee>? Employees { get; set; }
public Department? Department { get; set; }
}
protected class Contact
{
public required string Name { get; set; }
public List<string> PhoneNumbers { get; set; } = [];
}
protected class Employee
{
public required string Name { get; set; }
public List<string> PhoneNumbers { get; set; } = [];
public required Address Address { get; set; }
}
protected class Address
{
public required string Street { get; set; }
public required string City { get; set; }
public required string PostalCode { get; set; }
public required string Country { get; set; }
}
protected class Department
{
public required string Name { get; set; }
public decimal Budget { get; set; }
}
protected class WidgetWithDeepJson
{
public int Id { get; set; }
public required DeepData Deep { get; set; }
}
protected class DeepData
{
public required MiddleData Mid { get; set; }
}
protected class MiddleData
{
public List<DeepItem> Items { get; set; } = [];
}
protected class DeepItem
{
public required string Title { get; set; }
public List<InnerEntry> Inner { get; set; } = [];
public List<InnerEntry> Others { get; set; } = [];
}
protected class InnerEntry
{
public required string Value { get; set; }
}
protected class EntityWithNullableMeta
{
public int Id { get; set; }
public List<ItemWithMeta> Items { get; set; } = [];
}
protected class ItemWithMeta
{
public required string Name { get; set; }
public OptionalMeta? Meta { get; set; }
}
protected class OptionalMeta
{
public List<MetaEntry> Entries { get; set; } = [];
}
protected class MetaEntry
{
public required string Value { get; set; }
}
public abstract class ComplexCollectionJsonUpdateFixtureBase : SharedStoreFixtureBase<DbContext>
{
protected override string StoreName
=> "ComplexCollectionJsonUpdateTest";
public TestSqlLoggerFactory TestSqlLoggerFactory
=> (TestSqlLoggerFactory)ListLoggerFactory;
protected override bool ShouldLogCategory(string logCategory)
=> logCategory == DbLoggerCategory.Update.Name;
protected override Type ContextType
=> typeof(ComplexCollectionJsonContext);
protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
{
modelBuilder.Entity<CompanyWithComplexCollections>(b =>
{
b.Property(x => x.Id).ValueGeneratedNever();
b.ComplexCollection(
x => x.Contacts, cb =>
{
cb.ToJson();
cb.PrimitiveCollection(c => c.PhoneNumbers);
});
b.ComplexCollection(
x => x.Employees, cb =>
{
cb.ToJson();
cb.PrimitiveCollection(e => e.PhoneNumbers);
cb.ComplexProperty(e => e.Address);
});
b.ComplexProperty(
x => x.Department, cb =>
{
cb.ToJson();
});
});
modelBuilder.Entity<WidgetWithDeepJson>(b =>
{
b.Property(x => x.Id).ValueGeneratedNever();
b.ComplexProperty(
x => x.Deep, db =>
{
db.ToJson();
db.ComplexProperty(
d => d.Mid, mb =>
mb.ComplexCollection(
m => m.Items, ib =>
{
ib.ComplexCollection(i => i.Inner);
ib.ComplexCollection(i => i.Others);
}));
});
});
modelBuilder.Entity<EntityWithNullableMeta>(b =>
{
b.Property(x => x.Id).ValueGeneratedNever();
b.ComplexCollection(
x => x.Items, ib =>
{
ib.ToJson();
ib.ComplexProperty(
x => x.Meta, mb =>
mb.ComplexCollection(m => m.Entries));
});
});
}
protected override Task SeedAsync(DbContext context)
{
var company = new CompanyWithComplexCollections
{
Id = 1,
Name = "Test Company",
Contacts =
[
new Contact { Name = "First Contact", PhoneNumbers = ["555-1234", "555-5678"] },
new Contact { Name = "Second Contact", PhoneNumbers = ["555-9876", "555-5432"] }
],
Employees =
[
new Employee
{
Name = "Initial Employee",
PhoneNumbers = ["555-0001"],
Address = new Address
{
Street = "100 First St",
City = "Initial City",
PostalCode = "00001",
Country = "USA"
}
}
],
Department = new Department { Name = "Initial Department", Budget = 10000.00m }
};
context.Add(company);
var widget = new WidgetWithDeepJson
{
Id = 1,
Deep = new DeepData
{
Mid = new MiddleData
{
Items =
[
new DeepItem
{
Title = "Item1",
Inner = [new InnerEntry { Value = "inner-0" }],
Others = []
}
]
}
}
};
context.Add(widget);
var entityWithNullableMeta = new EntityWithNullableMeta
{
Id = 1,
Items =
[
new ItemWithMeta
{
Name = "Item1",
Meta = new OptionalMeta
{
Entries =
[
new MetaEntry { Value = "entry-0" },
new MetaEntry { Value = "entry-1" }
]
}
}
]
};
context.Add(entityWithNullableMeta);
return context.SaveChangesAsync();
}
}
}