-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathSqlServerModelBuilderGenericTest.cs
1226 lines (1000 loc) · 61.7 KB
/
SqlServerModelBuilderGenericTest.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;
// ReSharper disable InconsistentNaming
namespace Microsoft.EntityFrameworkCore.ModelBuilding
{
public class SqlServerModelBuilderGenericTest : ModelBuilderGenericTest
{
public class SqlServerGenericNonRelationship : GenericNonRelationship
{
[ConditionalFact]
public virtual void Index_has_a_filter_if_nonclustered_unique_with_nullable_properties()
{
var modelBuilder = CreateModelBuilder();
var entityTypeBuilder = modelBuilder
.Entity<Customer>();
var indexBuilder = entityTypeBuilder
.HasIndex(ix => ix.Name)
.IsUnique();
var entityType = modelBuilder.Model.FindEntityType(typeof(Customer));
var index = entityType.GetIndexes().Single();
Assert.Equal("[Name] IS NOT NULL", index.GetFilter());
indexBuilder.IsUnique(false);
Assert.Null(index.GetFilter());
indexBuilder.IsUnique();
Assert.Equal("[Name] IS NOT NULL", index.GetFilter());
indexBuilder.IsClustered();
Assert.Null(index.GetFilter());
indexBuilder.IsClustered(false);
Assert.Equal("[Name] IS NOT NULL", index.GetFilter());
entityTypeBuilder.Property(e => e.Name).IsRequired();
Assert.Null(index.GetFilter());
entityTypeBuilder.Property(e => e.Name).IsRequired(false);
Assert.Equal("[Name] IS NOT NULL", index.GetFilter());
entityTypeBuilder.Property(e => e.Name).HasColumnName("RelationalName");
Assert.Equal("[RelationalName] IS NOT NULL", index.GetFilter());
entityTypeBuilder.Property(e => e.Name).HasColumnName("SqlServerName");
Assert.Equal("[SqlServerName] IS NOT NULL", index.GetFilter());
entityTypeBuilder.Property(e => e.Name).HasColumnName(null);
Assert.Equal("[Name] IS NOT NULL", index.GetFilter());
indexBuilder.HasFilter("Foo");
Assert.Equal("Foo", index.GetFilter());
indexBuilder.HasFilter(null);
Assert.Null(index.GetFilter());
}
[ConditionalFact]
public virtual void Can_set_store_type_for_property_type()
{
var modelBuilder = CreateModelBuilder(c =>
{
c.Properties<int>().HaveColumnType("smallint");
c.Properties<string>().HaveColumnType("nchar(max)");
c.Properties(typeof(Nullable<>)).HavePrecision(2);
});
modelBuilder.Entity<Quarks>(
b =>
{
b.Property<int>("Charm");
b.Property<string>("Strange");
b.Property<int?>("Top");
b.Property<string>("Bottom");
});
var model = modelBuilder.FinalizeModel();
var entityType = model.FindEntityType(typeof(Quarks));
Assert.Equal("smallint", entityType.FindProperty(Customer.IdProperty.Name).GetColumnType());
Assert.Equal("smallint", entityType.FindProperty("Up").GetColumnType());
Assert.Equal("nchar(max)", entityType.FindProperty("Down").GetColumnType());
var charm = entityType.FindProperty("Charm");
Assert.Equal("smallint", charm.GetColumnType());
Assert.Null(charm.GetPrecision());
Assert.Equal("nchar(max)", entityType.FindProperty("Strange").GetColumnType());
var top = entityType.FindProperty("Top");
Assert.Equal("smallint", top.GetColumnType());
Assert.Equal(2, top.GetPrecision());
Assert.Equal("nchar(max)", entityType.FindProperty("Bottom").GetColumnType());
}
[ConditionalFact]
public virtual void Can_set_fixed_length_for_property_type()
{
var modelBuilder = CreateModelBuilder(c =>
{
c.Properties<int>().AreFixedLength(false);
c.Properties<string>().AreFixedLength();
});
modelBuilder.Entity<Quarks>(
b =>
{
b.Property<int>("Charm");
b.Property<string>("Strange");
b.Property<int>("Top");
b.Property<string>("Bottom");
});
var model = modelBuilder.FinalizeModel();
var entityType = model.FindEntityType(typeof(Quarks));
Assert.False(entityType.FindProperty(Customer.IdProperty.Name).IsFixedLength());
Assert.False(entityType.FindProperty("Up").IsFixedLength());
Assert.True(entityType.FindProperty("Down").IsFixedLength());
Assert.False(entityType.FindProperty("Charm").IsFixedLength());
Assert.True(entityType.FindProperty("Strange").IsFixedLength());
Assert.False(entityType.FindProperty("Top").IsFixedLength());
Assert.True(entityType.FindProperty("Bottom").IsFixedLength());
}
[ConditionalFact]
public virtual void Can_set_collation_for_property_type()
{
var modelBuilder = CreateModelBuilder(c =>
{
c.Properties<int>().UseCollation("Latin1_General_CS_AS_KS_WS");
c.Properties<string>().UseCollation("Latin1_General_BIN");
});
modelBuilder.Entity<Quarks>(
b =>
{
b.Property<int>("Charm");
b.Property<string>("Strange");
b.Property<int>("Top");
b.Property<string>("Bottom");
});
var model = modelBuilder.FinalizeModel();
var entityType = model.FindEntityType(typeof(Quarks));
Assert.Equal("Latin1_General_CS_AS_KS_WS", entityType.FindProperty(Customer.IdProperty.Name).GetCollation());
Assert.Equal("Latin1_General_CS_AS_KS_WS", entityType.FindProperty("Up").GetCollation());
Assert.Equal("Latin1_General_BIN", entityType.FindProperty("Down").GetCollation());
Assert.Equal("Latin1_General_CS_AS_KS_WS", entityType.FindProperty("Charm").GetCollation());
Assert.Equal("Latin1_General_BIN", entityType.FindProperty("Strange").GetCollation());
Assert.Equal("Latin1_General_CS_AS_KS_WS", entityType.FindProperty("Top").GetCollation());
Assert.Equal("Latin1_General_BIN", entityType.FindProperty("Bottom").GetCollation());
}
protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)
=> CreateTestModelBuilder(SqlServerTestHelpers.Instance, configure);
}
public class SqlServerGenericInheritance : GenericInheritance
{
[ConditionalFact] // #7240
public void Can_use_shadow_FK_that_collides_with_convention_shadow_FK_on_other_derived_type()
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Entity<Child>();
modelBuilder.Entity<Parent>()
.HasOne(p => p.A)
.WithOne()
.HasForeignKey<DisjointChildSubclass1>("ParentId");
var model = modelBuilder.FinalizeModel();
var property1 = model.FindEntityType(typeof(DisjointChildSubclass1)).FindProperty("ParentId");
Assert.True(property1.IsForeignKey());
Assert.Equal("ParentId", property1.GetColumnBaseName());
var property2 = model.FindEntityType(typeof(DisjointChildSubclass2)).FindProperty("ParentId");
Assert.True(property2.IsForeignKey());
Assert.Equal("DisjointChildSubclass2_ParentId", property2.GetColumnBaseName());
}
[ConditionalFact]
public void Inherited_clr_properties_are_mapped_to_the_same_column()
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Entity<ChildBase>();
modelBuilder.Ignore<Child>();
modelBuilder.Entity<DisjointChildSubclass1>();
modelBuilder.Entity<DisjointChildSubclass2>();
var model = modelBuilder.FinalizeModel();
var property1 = model.FindEntityType(typeof(DisjointChildSubclass1)).FindProperty(nameof(Child.Name));
Assert.Equal(nameof(Child.Name), property1.GetColumnBaseName());
var property2 = model.FindEntityType(typeof(DisjointChildSubclass2)).FindProperty(nameof(Child.Name));
Assert.Equal(nameof(Child.Name), property2.GetColumnBaseName());
}
[ConditionalFact] //Issue#10659
public void Index_convention_run_for_fk_when_derived_type_discovered_before_base_type()
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Ignore<Order>();
modelBuilder.Entity<CustomerDetails>();
modelBuilder.Entity<DetailsBase>();
var index = modelBuilder.Model.FindEntityType(typeof(CustomerDetails)).GetIndexes().Single();
Assert.Equal("[CustomerId] IS NOT NULL", index.GetFilter());
}
[ConditionalFact]
public void Index_convention_sets_filter_for_unique_index_when_base_type_changed()
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Ignore<Customer>();
modelBuilder.Entity<CustomerDetails>()
.HasIndex(e => e.CustomerId)
.IsUnique();
modelBuilder.Entity<DetailsBase>();
var index = modelBuilder.Model.FindEntityType(typeof(CustomerDetails)).GetIndexes().Single();
Assert.Equal("[CustomerId] IS NOT NULL", index.GetFilter());
modelBuilder.Ignore<DetailsBase>();
Assert.Null(index.GetFilter());
}
[ConditionalFact]
public virtual void TPT_identifying_FK_are_created_only_on_declaring_type()
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Entity<BigMak>()
.Ignore(b => b.Bun)
.Ignore(b => b.Pickles);
modelBuilder.Entity<Ingredient>(b =>
{
b.ToTable("Ingredients");
b.Ignore(i => i.BigMak);
});
modelBuilder.Entity<Bun>(b =>
{
b.ToTable("Buns");
b.HasOne(i => i.BigMak).WithOne().HasForeignKey<Bun>(i => i.Id);
});
modelBuilder.Entity<SesameBun>(b =>
{
b.ToTable("SesameBuns");
});
var model = modelBuilder.FinalizeModel();
var principalType = model.FindEntityType(typeof(BigMak));
Assert.Empty(principalType.GetForeignKeys());
Assert.Empty(principalType.GetIndexes());
var ingredientType = model.FindEntityType(typeof(Ingredient));
var bunType = model.FindEntityType(typeof(Bun));
Assert.Empty(bunType.GetIndexes());
var bunFk = bunType.GetDeclaredForeignKeys().Single(fk => !fk.IsBaseLinking());
Assert.Equal("FK_Buns_BigMak_Id", bunFk.GetConstraintName());
Assert.Equal("FK_Buns_BigMak_Id", bunFk.GetConstraintName(
StoreObjectIdentifier.Create(bunType, StoreObjectType.Table).Value,
StoreObjectIdentifier.Create(principalType, StoreObjectType.Table).Value));
Assert.Single(bunFk.GetMappedConstraints());
var bunLinkingFk = bunType.GetDeclaredForeignKeys().Single(fk => fk.IsBaseLinking());
Assert.Equal("FK_Buns_Ingredients_Id", bunLinkingFk.GetConstraintName());
Assert.Equal("FK_Buns_Ingredients_Id", bunLinkingFk.GetConstraintName(
StoreObjectIdentifier.Create(bunType, StoreObjectType.Table).Value,
StoreObjectIdentifier.Create(ingredientType, StoreObjectType.Table).Value));
Assert.Single(bunLinkingFk.GetMappedConstraints());
var sesameBunType = model.FindEntityType(typeof(SesameBun));
Assert.Empty(sesameBunType.GetIndexes());
var sesameBunFk = sesameBunType.GetDeclaredForeignKeys().Single();
Assert.True(sesameBunFk.IsBaseLinking());
Assert.Equal("FK_SesameBuns_Buns_Id", sesameBunFk.GetConstraintName());
Assert.Equal("FK_SesameBuns_Buns_Id", sesameBunFk.GetConstraintName(
StoreObjectIdentifier.Create(sesameBunType, StoreObjectType.Table).Value,
StoreObjectIdentifier.Create(bunType, StoreObjectType.Table).Value));
Assert.Single(sesameBunFk.GetMappedConstraints());
}
[ConditionalFact]
public virtual void TPT_index_can_use_inherited_properties()
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Entity<BigMak>()
.Ignore(b => b.Bun)
.Ignore(b => b.Pickles);
modelBuilder.Entity<Ingredient>(b =>
{
b.ToTable("Ingredients");
b.Property<int?>("NullableProp");
b.Ignore(i => i.BigMak);
});
modelBuilder.Entity<Bun>(b =>
{
b.ToTable("Buns");
b.HasIndex(bun => bun.BurgerId);
b.HasIndex("NullableProp");
b.HasOne(i => i.BigMak).WithOne().HasForeignKey<Bun>(i => i.Id);
});
var model = modelBuilder.FinalizeModel();
var bunType = model.FindEntityType(typeof(Bun));
Assert.All(bunType.GetIndexes(), i => Assert.Null(i.GetFilter()));
}
[ConditionalFact]
public void Can_add_check_constraints()
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Entity<Child>()
.HasBaseType(null)
.HasCheckConstraint("LargeId", "Id > 1000", c => c.HasName("CK_LargeId"));
modelBuilder.Entity<ChildBase>()
.HasCheckConstraint("PositiveId", "Id > 0")
.HasCheckConstraint("LargeId", "Id > 1000");
modelBuilder.Entity<Child>()
.HasBaseType<ChildBase>();
modelBuilder.Entity<DisjointChildSubclass1>();
var model = modelBuilder.FinalizeModel();
var @base = model.FindEntityType(typeof(ChildBase));
Assert.Equal(2, @base.GetCheckConstraints().Count());
var firstCheckConstraint = @base.FindCheckConstraint("PositiveId");
Assert.Equal("PositiveId", firstCheckConstraint.ModelName);
Assert.Equal("Id > 0", firstCheckConstraint.Sql);
Assert.Equal("CK_ChildBase_PositiveId", firstCheckConstraint.Name);
var secondCheckConstraint = @base.FindCheckConstraint("LargeId");
Assert.Equal("LargeId", secondCheckConstraint.ModelName);
Assert.Equal("Id > 1000", secondCheckConstraint.Sql);
Assert.Equal("CK_LargeId", secondCheckConstraint.Name);
var child = model.FindEntityType(typeof(Child));
Assert.Equal(@base.GetCheckConstraints(), child.GetCheckConstraints());
Assert.Empty(child.GetDeclaredCheckConstraints());
}
[ConditionalFact]
public void Adding_conflicting_check_constraint_to_derived_type_throws()
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Entity<ChildBase>()
.HasCheckConstraint("LargeId", "Id > 100", c => c.HasName("CK_LargeId"));
Assert.Equal(
RelationalStrings.DuplicateCheckConstraint("LargeId", nameof(Child), nameof(ChildBase)),
Assert.Throws<InvalidOperationException>(
() => modelBuilder.Entity<Child>().HasCheckConstraint("LargeId", "Id > 1000")).Message);
}
[ConditionalFact]
public void Adding_conflicting_check_constraint_to_derived_type_before_base_throws()
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Entity<Child>()
.HasBaseType(null)
.HasCheckConstraint("LargeId", "Id > 1000");
modelBuilder.Entity<ChildBase>()
.HasCheckConstraint("LargeId", "Id > 100", c => c.HasName("CK_LargeId"));
Assert.Equal(
RelationalStrings.DuplicateCheckConstraint("LargeId", nameof(Child), nameof(ChildBase)),
Assert.Throws<InvalidOperationException>(
() => modelBuilder.Entity<Child>().HasBaseType<ChildBase>()).Message);
}
public class Parent
{
public int Id { get; set; }
public DisjointChildSubclass1 A { get; set; }
public IList<DisjointChildSubclass2> B { get; set; }
}
public abstract class ChildBase
{
public int Id { get; set; }
}
public abstract class Child : ChildBase
{
public string Name { get; set; }
}
public class DisjointChildSubclass1 : Child
{
}
public class DisjointChildSubclass2 : Child
{
}
protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)
=> CreateTestModelBuilder(SqlServerTestHelpers.Instance, configure);
}
public class SqlServerGenericOneToMany : GenericOneToMany
{
protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)
=> CreateTestModelBuilder(SqlServerTestHelpers.Instance, configure);
}
public class SqlServerGenericManyToOne : GenericManyToOne
{
protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)
=> CreateTestModelBuilder(SqlServerTestHelpers.Instance, configure);
}
public class SqlServerGenericOneToOne : GenericOneToOne
{
protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)
=> CreateTestModelBuilder(SqlServerTestHelpers.Instance, configure);
}
public class SqlServerGenericManyToMany : GenericManyToMany
{
[ConditionalFact]
public virtual void Join_entity_type_uses_same_schema()
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Entity<Category>().ToTable("Category", "mySchema").Ignore(c => c.ProductCategories);
modelBuilder.Entity<Product>().ToTable("Product", "mySchema");
modelBuilder.Entity<CategoryBase>();
var model = modelBuilder.FinalizeModel();
var productType = model.FindEntityType(typeof(Product));
var categoryType = model.FindEntityType(typeof(Category));
var categoriesNavigation = productType.GetSkipNavigations().Single();
var productsNavigation = categoryType.GetSkipNavigations().Single();
var categoriesFk = categoriesNavigation.ForeignKey;
var productsFk = productsNavigation.ForeignKey;
var productCategoryType = categoriesFk.DeclaringEntityType;
Assert.Equal(typeof(Dictionary<string, object>), productCategoryType.ClrType);
Assert.Equal("mySchema", productCategoryType.GetSchema());
Assert.Same(categoriesFk, productCategoryType.GetForeignKeys().Last());
Assert.Same(productsFk, productCategoryType.GetForeignKeys().First());
Assert.Equal(2, productCategoryType.GetForeignKeys().Count());
}
[ConditionalFact]
public virtual void Join_entity_type_uses_default_schema_if_related_are_different()
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Entity<Category>().ToTable("Category").Ignore(c => c.ProductCategories);
modelBuilder.Entity<Product>().ToTable("Product", "dbo");
modelBuilder.Entity<CategoryBase>();
var model = modelBuilder.FinalizeModel();
var productType = model.FindEntityType(typeof(Product));
var categoryType = model.FindEntityType(typeof(Category));
var categoriesNavigation = productType.GetSkipNavigations().Single();
var productsNavigation = categoryType.GetSkipNavigations().Single();
var categoriesFk = categoriesNavigation.ForeignKey;
var productsFk = productsNavigation.ForeignKey;
var productCategoryType = categoriesFk.DeclaringEntityType;
Assert.Equal(typeof(Dictionary<string, object>), productCategoryType.ClrType);
Assert.Null(productCategoryType.GetSchema());
Assert.Same(categoriesFk, productCategoryType.GetForeignKeys().Last());
Assert.Same(productsFk, productCategoryType.GetForeignKeys().First());
Assert.Equal(2, productCategoryType.GetForeignKeys().Count());
}
protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)
=> CreateTestModelBuilder(SqlServerTestHelpers.Instance, configure);
}
public class SqlServerGenericOwnedTypes : GenericOwnedTypes
{
[ConditionalFact]
public virtual void Owned_types_use_table_splitting_by_default()
{
var modelBuilder = CreateModelBuilder();
var model = modelBuilder.Model;
modelBuilder.Entity<Book>().OwnsOne(b => b.AlternateLabel)
.Ignore(l => l.Book)
.OwnsOne(l => l.AnotherBookLabel)
.Ignore(l => l.Book)
.OwnsOne(s => s.SpecialBookLabel)
.Ignore(l => l.Book)
.Ignore(l => l.BookLabel);
modelBuilder.Entity<Book>().OwnsOne(b => b.Label)
.Ignore(l => l.Book)
.OwnsOne(l => l.SpecialBookLabel)
.Ignore(l => l.Book)
.OwnsOne(a => a.AnotherBookLabel)
.Ignore(l => l.Book);
modelBuilder.Entity<Book>().OwnsOne(b => b.Label)
.OwnsOne(l => l.AnotherBookLabel)
.Ignore(l => l.Book)
.OwnsOne(a => a.SpecialBookLabel)
.Ignore(l => l.Book)
.Ignore(l => l.BookLabel);
modelBuilder.Entity<Book>().OwnsOne(b => b.AlternateLabel)
.OwnsOne(l => l.SpecialBookLabel)
.Ignore(l => l.Book)
.OwnsOne(s => s.AnotherBookLabel)
.Ignore(l => l.Book);
var book = model.FindEntityType(typeof(Book));
var bookOwnership1 = book.FindNavigation(nameof(Book.Label)).ForeignKey;
var bookOwnership2 = book.FindNavigation(nameof(Book.AlternateLabel)).ForeignKey;
var bookLabel1Ownership1 = bookOwnership1.DeclaringEntityType.FindNavigation(nameof(BookLabel.AnotherBookLabel)).ForeignKey;
var bookLabel1Ownership2 = bookOwnership1.DeclaringEntityType.FindNavigation(nameof(BookLabel.SpecialBookLabel)).ForeignKey;
var bookLabel2Ownership1 = bookOwnership2.DeclaringEntityType.FindNavigation(nameof(BookLabel.AnotherBookLabel)).ForeignKey;
var bookLabel2Ownership2 = bookOwnership2.DeclaringEntityType.FindNavigation(nameof(BookLabel.SpecialBookLabel)).ForeignKey;
Assert.Equal(book.GetTableName(), bookOwnership1.DeclaringEntityType.GetTableName());
Assert.Equal(book.GetTableName(), bookOwnership2.DeclaringEntityType.GetTableName());
Assert.Equal(book.GetTableName(), bookLabel1Ownership1.DeclaringEntityType.GetTableName());
Assert.Equal(book.GetTableName(), bookLabel1Ownership2.DeclaringEntityType.GetTableName());
Assert.Equal(book.GetTableName(), bookLabel2Ownership1.DeclaringEntityType.GetTableName());
Assert.Equal(book.GetTableName(), bookLabel2Ownership2.DeclaringEntityType.GetTableName());
Assert.NotSame(bookOwnership1.DeclaringEntityType, bookOwnership2.DeclaringEntityType);
Assert.Single(bookOwnership1.DeclaringEntityType.GetForeignKeys());
Assert.Single(bookOwnership1.DeclaringEntityType.GetForeignKeys());
Assert.NotSame(bookLabel1Ownership1.DeclaringEntityType, bookLabel2Ownership1.DeclaringEntityType);
Assert.NotSame(bookLabel1Ownership2.DeclaringEntityType, bookLabel2Ownership2.DeclaringEntityType);
Assert.Single(bookLabel1Ownership1.DeclaringEntityType.GetForeignKeys());
Assert.Single(bookLabel1Ownership2.DeclaringEntityType.GetForeignKeys());
Assert.Single(bookLabel2Ownership1.DeclaringEntityType.GetForeignKeys());
Assert.Single(bookLabel2Ownership2.DeclaringEntityType.GetForeignKeys());
Assert.Equal(2, model.GetEntityTypes().Count(e => e.ClrType == typeof(BookLabel)));
Assert.Equal(4, model.GetEntityTypes().Count(e => e.ClrType == typeof(AnotherBookLabel)));
Assert.Equal(4, model.GetEntityTypes().Count(e => e.ClrType == typeof(SpecialBookLabel)));
Assert.Null(
bookOwnership1.DeclaringEntityType.FindProperty(nameof(BookLabel.Id))
.GetColumnName(StoreObjectIdentifier.Table("Label", null)));
Assert.Null(
bookLabel2Ownership1.DeclaringEntityType.FindProperty(nameof(BookLabel.Id))
.GetColumnName(StoreObjectIdentifier.Table("AlternateLabel", null)));
modelBuilder.Entity<Book>().OwnsOne(b => b.Label).ToTable("Label");
modelBuilder.Entity<Book>().OwnsOne(b => b.AlternateLabel).ToTable("AlternateLabel");
modelBuilder.FinalizeModel();
Assert.Equal(
nameof(BookLabel.Id),
bookOwnership1.DeclaringEntityType.FindProperty(nameof(BookLabel.Id))
.GetColumnName(StoreObjectIdentifier.Table("Label", null)));
Assert.Equal(
nameof(BookLabel.AnotherBookLabel) + "_" + nameof(BookLabel.Id),
bookLabel2Ownership1.DeclaringEntityType.FindProperty(nameof(BookLabel.Id))
.GetColumnName(StoreObjectIdentifier.Table("AlternateLabel", null)));
}
[ConditionalFact]
public virtual void Owned_types_can_be_mapped_to_different_tables()
{
var modelBuilder = CreateModelBuilder();
var model = modelBuilder.Model;
modelBuilder.Entity<Book>(
bb =>
{
bb.ToTable("BT", "BS", t => t.ExcludeFromMigrations());
bb.OwnsOne(
b => b.AlternateLabel, tb =>
{
tb.Ignore(l => l.Book);
tb.WithOwner()
.HasConstraintName("AlternateLabelFK");
tb.ToTable("TT", "TS");
tb.IsMemoryOptimized();
tb.OwnsOne(
l => l.AnotherBookLabel, ab =>
{
ab.Ignore(l => l.Book);
ab.ToTable("AT1", "AS1", t => t.ExcludeFromMigrations(false));
ab.OwnsOne(s => s.SpecialBookLabel)
.ToTable("ST11", "SS11")
.Ignore(l => l.Book)
.Ignore(l => l.BookLabel);
ab.OwnedEntityType.FindNavigation(nameof(BookLabel.SpecialBookLabel))
.AddAnnotation("Foo", "Bar");
});
tb.OwnsOne(
l => l.SpecialBookLabel, sb =>
{
sb.Ignore(l => l.Book);
sb.ToTable("ST2", "SS2");
sb.OwnsOne(s => s.AnotherBookLabel)
.ToTable("AT21", "AS21")
.Ignore(l => l.Book);
});
});
bb.OwnsOne(
b => b.Label, lb =>
{
lb.Ignore(l => l.Book);
lb.ToTable("LT", "LS");
lb.OwnsOne(
l => l.SpecialBookLabel, sb =>
{
sb.Ignore(l => l.Book);
sb.ToTable("ST1", "SS1");
sb.OwnsOne(a => a.AnotherBookLabel)
.ToTable("AT11", "AS11")
.Ignore(l => l.Book);
});
lb.OwnsOne(
l => l.AnotherBookLabel, ab =>
{
ab.Ignore(l => l.Book);
ab.ToTable("AT2", "AS2");
ab.OwnsOne(a => a.SpecialBookLabel)
.ToTable("ST21", "SS21")
.Ignore(l => l.BookLabel)
.Ignore(l => l.Book);
});
});
});
modelBuilder.FinalizeModel();
var book = model.FindEntityType(typeof(Book));
var bookOwnership1 = book.FindNavigation(nameof(Book.Label)).ForeignKey;
var bookOwnership2 = book.FindNavigation(nameof(Book.AlternateLabel)).ForeignKey;
var bookLabel1Ownership1 = bookOwnership1.DeclaringEntityType.FindNavigation(nameof(BookLabel.AnotherBookLabel)).ForeignKey;
var bookLabel1Ownership2 = bookOwnership1.DeclaringEntityType.FindNavigation(nameof(BookLabel.SpecialBookLabel)).ForeignKey;
var bookLabel2Ownership1 = bookOwnership2.DeclaringEntityType.FindNavigation(nameof(BookLabel.AnotherBookLabel)).ForeignKey;
var bookLabel2Ownership2 = bookOwnership2.DeclaringEntityType.FindNavigation(nameof(BookLabel.SpecialBookLabel)).ForeignKey;
var bookLabel1Ownership11 = bookLabel1Ownership1.DeclaringEntityType.FindNavigation(nameof(BookLabel.SpecialBookLabel))
.ForeignKey;
var bookLabel1Ownership21 = bookLabel1Ownership2.DeclaringEntityType.FindNavigation(nameof(BookLabel.AnotherBookLabel))
.ForeignKey;
var bookLabel2Ownership11 = bookLabel2Ownership1.DeclaringEntityType.FindNavigation(nameof(BookLabel.SpecialBookLabel))
.ForeignKey;
var bookLabel2Ownership21 = bookLabel2Ownership2.DeclaringEntityType.FindNavigation(nameof(BookLabel.AnotherBookLabel))
.ForeignKey;
Assert.Equal("AlternateLabelFK", bookOwnership2.GetConstraintName());
Assert.Equal("BS", book.GetSchema());
Assert.Equal("BT", book.GetTableName());
Assert.True(book.IsTableExcludedFromMigrations());
Assert.Equal("LS", bookOwnership1.DeclaringEntityType.GetSchema());
Assert.Equal("LT", bookOwnership1.DeclaringEntityType.GetTableName());
Assert.False(bookOwnership1.DeclaringEntityType.IsMemoryOptimized());
Assert.True(bookOwnership1.DeclaringEntityType.IsTableExcludedFromMigrations());
Assert.Equal("TS", bookOwnership2.DeclaringEntityType.GetSchema());
Assert.Equal("TT", bookOwnership2.DeclaringEntityType.GetTableName());
Assert.True(bookOwnership2.DeclaringEntityType.IsMemoryOptimized());
Assert.True(bookOwnership2.DeclaringEntityType.IsTableExcludedFromMigrations());
Assert.Equal("AS2", bookLabel1Ownership1.DeclaringEntityType.GetSchema());
Assert.Equal("AT2", bookLabel1Ownership1.DeclaringEntityType.GetTableName());
Assert.Equal("SS1", bookLabel1Ownership2.DeclaringEntityType.GetSchema());
Assert.Equal("ST1", bookLabel1Ownership2.DeclaringEntityType.GetTableName());
Assert.Equal("AS1", bookLabel2Ownership1.DeclaringEntityType.GetSchema());
Assert.Equal("AT1", bookLabel2Ownership1.DeclaringEntityType.GetTableName());
Assert.False(bookLabel2Ownership1.DeclaringEntityType.IsTableExcludedFromMigrations());
Assert.Equal("SS2", bookLabel2Ownership2.DeclaringEntityType.GetSchema());
Assert.Equal("ST2", bookLabel2Ownership2.DeclaringEntityType.GetTableName());
Assert.Equal("SS21", bookLabel1Ownership11.DeclaringEntityType.GetSchema());
Assert.Equal("ST21", bookLabel1Ownership11.DeclaringEntityType.GetTableName());
Assert.Equal("AS11", bookLabel1Ownership21.DeclaringEntityType.GetSchema());
Assert.Equal("AT11", bookLabel1Ownership21.DeclaringEntityType.GetTableName());
Assert.Equal("SS11", bookLabel2Ownership11.DeclaringEntityType.GetSchema());
Assert.Equal("ST11", bookLabel2Ownership11.DeclaringEntityType.GetTableName());
Assert.Equal("AS21", bookLabel2Ownership21.DeclaringEntityType.GetSchema());
Assert.Equal("AT21", bookLabel2Ownership21.DeclaringEntityType.GetTableName());
Assert.Equal("Bar", bookLabel2Ownership11.PrincipalToDependent["Foo"]);
Assert.NotSame(bookOwnership1.DeclaringEntityType, bookOwnership2.DeclaringEntityType);
Assert.Single(bookOwnership1.DeclaringEntityType.GetForeignKeys());
Assert.Single(bookOwnership2.DeclaringEntityType.GetForeignKeys());
Assert.NotSame(bookLabel1Ownership1.DeclaringEntityType, bookLabel2Ownership1.DeclaringEntityType);
Assert.NotSame(bookLabel1Ownership2.DeclaringEntityType, bookLabel2Ownership2.DeclaringEntityType);
Assert.Single(bookLabel1Ownership1.DeclaringEntityType.GetForeignKeys());
Assert.Single(bookLabel1Ownership2.DeclaringEntityType.GetForeignKeys());
Assert.Single(bookLabel2Ownership1.DeclaringEntityType.GetForeignKeys());
Assert.Single(bookLabel2Ownership2.DeclaringEntityType.GetForeignKeys());
Assert.NotSame(bookLabel1Ownership11.DeclaringEntityType, bookLabel2Ownership11.DeclaringEntityType);
Assert.NotSame(bookLabel1Ownership21.DeclaringEntityType, bookLabel2Ownership21.DeclaringEntityType);
Assert.Single(bookLabel1Ownership11.DeclaringEntityType.GetForeignKeys());
Assert.Single(bookLabel1Ownership21.DeclaringEntityType.GetForeignKeys());
Assert.Single(bookLabel2Ownership11.DeclaringEntityType.GetForeignKeys());
Assert.Single(bookLabel2Ownership21.DeclaringEntityType.GetForeignKeys());
Assert.Equal(2, model.GetEntityTypes().Count(e => e.ClrType == typeof(BookLabel)));
Assert.Equal(4, model.GetEntityTypes().Count(e => e.ClrType == typeof(AnotherBookLabel)));
Assert.Equal(4, model.GetEntityTypes().Count(e => e.ClrType == typeof(SpecialBookLabel)));
Assert.Equal(ValueGenerated.Never, bookOwnership1.DeclaringEntityType.FindPrimaryKey().Properties.Single().ValueGenerated);
Assert.Equal(ValueGenerated.Never, bookOwnership2.DeclaringEntityType.FindPrimaryKey().Properties.Single().ValueGenerated);
Assert.Equal(
ValueGenerated.Never, bookLabel1Ownership1.DeclaringEntityType.FindPrimaryKey().Properties.Single().ValueGenerated);
Assert.Equal(
ValueGenerated.Never, bookLabel1Ownership2.DeclaringEntityType.FindPrimaryKey().Properties.Single().ValueGenerated);
Assert.Equal(
ValueGenerated.Never, bookLabel2Ownership1.DeclaringEntityType.FindPrimaryKey().Properties.Single().ValueGenerated);
Assert.Equal(
ValueGenerated.Never, bookLabel2Ownership2.DeclaringEntityType.FindPrimaryKey().Properties.Single().ValueGenerated);
Assert.Equal(
ValueGenerated.Never, bookLabel1Ownership11.DeclaringEntityType.FindPrimaryKey().Properties.Single().ValueGenerated);
Assert.Equal(
ValueGenerated.Never, bookLabel1Ownership21.DeclaringEntityType.FindPrimaryKey().Properties.Single().ValueGenerated);
Assert.Equal(
ValueGenerated.Never, bookLabel2Ownership11.DeclaringEntityType.FindPrimaryKey().Properties.Single().ValueGenerated);
Assert.Equal(
ValueGenerated.Never, bookLabel2Ownership21.DeclaringEntityType.FindPrimaryKey().Properties.Single().ValueGenerated);
}
[ConditionalFact]
public virtual void Owned_type_collections_can_be_mapped_to_different_tables()
{
var modelBuilder = CreateModelBuilder();
var model = modelBuilder.Model;
modelBuilder.Entity<Customer>().OwnsMany(
c => c.Orders,
r =>
{
r.HasKey(o => o.OrderId);
r.IsMemoryOptimized();
r.Ignore(o => o.OrderCombination);
r.Ignore(o => o.Details);
});
var ownership = model.FindEntityType(typeof(Customer)).FindNavigation(nameof(Customer.Orders)).ForeignKey;
var owned = ownership.DeclaringEntityType;
Assert.True(ownership.IsOwnership);
Assert.Equal(nameof(Order.Customer), ownership.DependentToPrincipal.Name);
Assert.Equal("FK_Order_Customer_CustomerId", ownership.GetConstraintName());
Assert.Single(owned.GetForeignKeys());
Assert.Single(owned.GetIndexes());
Assert.Equal(
new[] { nameof(Order.OrderId), nameof(Order.AnotherCustomerId), nameof(Order.CustomerId) },
owned.GetProperties().Select(p => p.GetColumnBaseName()));
Assert.Equal(nameof(Order), owned.GetTableName());
Assert.Null(owned.GetSchema());
Assert.True(owned.IsMemoryOptimized());
modelBuilder.Entity<Customer>().OwnsMany(
c => c.Orders,
r =>
{
r.WithOwner(o => o.Customer).HasConstraintName("Owned");
r.ToTable("bar", "foo");
});
Assert.Equal("bar", owned.GetTableName());
Assert.Equal("foo", owned.GetSchema());
Assert.Equal("Owned", ownership.GetConstraintName());
modelBuilder.Entity<Customer>().OwnsMany(
c => c.Orders,
r => r.ToTable("blah"));
modelBuilder.FinalizeModel();
Assert.Equal("blah", owned.GetTableName());
Assert.Null(owned.GetSchema());
}
[ConditionalFact]
public override void Can_configure_owned_type()
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Ignore<Customer>();
modelBuilder.Ignore<Product>();
var ownedBuilder = modelBuilder.Entity<OtherCustomer>().OwnsOne(c => c.Details)
.ToTable("OtherCustomerDetails")
.HasCheckConstraint("CK_CustomerDetails_T", "AlternateKey <> 0", c => c.HasName("CK_Guid"));
ownedBuilder.Property(d => d.CustomerId);
ownedBuilder.HasIndex(d => d.CustomerId);
ownedBuilder.WithOwner(d => (OtherCustomer)d.Customer)
.HasPrincipalKey(c => c.AlternateKey);
modelBuilder.Entity<SpecialCustomer>().OwnsOne(c => c.Details, b =>
{
b.ToTable("SpecialCustomerDetails");
b.HasCheckConstraint("CK_CustomerDetails_T", "AlternateKey <> 0", c => c.HasName("CK_Guid"));
b.Property(d => d.CustomerId);
b.HasIndex(d => d.CustomerId);
b.WithOwner(d => (SpecialCustomer)d.Customer)
.HasPrincipalKey(c => c.AlternateKey);
});
var model = modelBuilder.FinalizeModel();
var owner1 = model.FindEntityType(typeof(OtherCustomer));
Assert.Equal(typeof(OtherCustomer).FullName, owner1.Name);
AssertOwnership(owner1);
var owner2 = model.FindEntityType(typeof(SpecialCustomer));
Assert.Equal(typeof(SpecialCustomer).FullName, owner2.Name);
AssertOwnership(owner2);
Assert.Null(model.FindEntityType(typeof(CustomerDetails)));
Assert.Equal(2, model.GetEntityTypes().Count(e => e.ClrType == typeof(CustomerDetails)));
static void AssertOwnership(IEntityType owner)
{
var ownership1 = owner.FindNavigation(nameof(Customer.Details)).ForeignKey;
Assert.True(ownership1.IsOwnership);
Assert.Equal(nameof(Customer.Details), ownership1.PrincipalToDependent.Name);
Assert.Equal("CustomerAlternateKey", ownership1.Properties.Single().Name);
Assert.Equal(nameof(Customer.AlternateKey), ownership1.PrincipalKey.Properties.Single().Name);
var owned = ownership1.DeclaringEntityType;
Assert.Equal(owner.ShortName() + "Details", owned.GetTableName());
var checkConstraint = owned.GetCheckConstraints().Single();
Assert.Same(owned, checkConstraint.EntityType);
Assert.Equal("CK_CustomerDetails_T", checkConstraint.ModelName);
Assert.Equal("AlternateKey <> 0", checkConstraint.Sql);
Assert.Equal("CK_Guid", checkConstraint.Name);
Assert.Single(owned.GetForeignKeys());
var index = owned.GetIndexes().Single();
Assert.Same(owned, index.DeclaringEntityType);
Assert.Equal(nameof(CustomerDetails.CustomerId), index.Properties.Single().Name);
Assert.Equal(
new[] { "CustomerAlternateKey", nameof(CustomerDetails.CustomerId), nameof(CustomerDetails.Id) },
owned.GetProperties().Select(p => p.Name));
}
}
[ConditionalFact]
public override void Can_configure_owned_type_key()
{
var modelBuilder = CreateModelBuilder();
var model = modelBuilder.Model;
modelBuilder.Entity<Customer>().OwnsOne(c => c.Details)
.ToTable("Details")
.HasKey(c => c.Id);
modelBuilder.FinalizeModel();
var owner = model.FindEntityType(typeof(Customer));
var owned = owner.FindNavigation(nameof(Customer.Details)).ForeignKey.DeclaringEntityType;
Assert.Equal(
new[] { nameof(CustomerDetails.Id), nameof(CustomerDetails.CustomerId) },
owned.GetProperties().Select(p => p.Name).ToArray());
Assert.Equal(nameof(CustomerDetails.Id), owned.FindPrimaryKey().Properties.Single().Name);
}
[ConditionalFact]
public virtual void Temporal_table_default_settings()
{
var modelBuilder = CreateModelBuilder();
var model = modelBuilder.Model;
modelBuilder.Entity<Customer>().ToTable(tb => tb.IsTemporal());
modelBuilder.FinalizeModel();
var entity = model.FindEntityType(typeof(Customer));
Assert.True(entity.IsTemporal());
Assert.Equal("CustomerHistory", entity.GetHistoryTableName());
Assert.Null(entity.GetHistoryTableSchema());
var periodStart = entity.GetProperty(entity.GetPeriodStartPropertyName());
var periodEnd = entity.GetProperty(entity.GetPeriodEndPropertyName());
Assert.Equal("PeriodStart", periodStart.Name);
Assert.True(periodStart.IsShadowProperty());
Assert.Equal(typeof(DateTime), periodStart.ClrType);
Assert.Equal(ValueGenerated.OnAddOrUpdate, periodStart.ValueGenerated);
Assert.Equal("PeriodEnd", periodEnd.Name);
Assert.True(periodEnd.IsShadowProperty());
Assert.Equal(typeof(DateTime), periodEnd.ClrType);
Assert.Equal(ValueGenerated.OnAddOrUpdate, periodEnd.ValueGenerated);
}
[ConditionalFact]
public virtual void Temporal_table_with_history_table_configuration()
{
var modelBuilder = CreateModelBuilder();
var model = modelBuilder.Model;
modelBuilder.Entity<Customer>().ToTable(tb => tb.IsTemporal(ttb =>
{
ttb.UseHistoryTable("HistoryTable", "historySchema");
ttb.HasPeriodStart("MyPeriodStart").HasColumnName("PeriodStartColumn");
ttb.HasPeriodEnd("MyPeriodEnd").HasColumnName("PeriodEndColumn");
}));
modelBuilder.FinalizeModel();
var entity = model.FindEntityType(typeof(Customer));
Assert.True(entity.IsTemporal());
Assert.Equal(5, entity.GetProperties().Count());
Assert.Equal("HistoryTable", entity.GetHistoryTableName());
Assert.Equal("historySchema", entity.GetHistoryTableSchema());
var periodStart = entity.GetProperty(entity.GetPeriodStartPropertyName());
var periodEnd = entity.GetProperty(entity.GetPeriodEndPropertyName());
Assert.Equal("MyPeriodStart", periodStart.Name);
Assert.Equal("PeriodStartColumn", periodStart[RelationalAnnotationNames.ColumnName]);
Assert.True(periodStart.IsShadowProperty());
Assert.Equal(typeof(DateTime), periodStart.ClrType);
Assert.Equal(ValueGenerated.OnAddOrUpdate, periodStart.ValueGenerated);
Assert.Equal("MyPeriodEnd", periodEnd.Name);
Assert.Equal("PeriodEndColumn", periodEnd[RelationalAnnotationNames.ColumnName]);
Assert.True(periodEnd.IsShadowProperty());
Assert.Equal(typeof(DateTime), periodEnd.ClrType);
Assert.Equal(ValueGenerated.OnAddOrUpdate, periodEnd.ValueGenerated);
}
[ConditionalFact]
public virtual void Temporal_table_with_changed_configuration()
{
var modelBuilder = CreateModelBuilder();
var model = modelBuilder.Model;
modelBuilder.Entity<Customer>().ToTable(tb => tb.IsTemporal(ttb =>
{
ttb.UseHistoryTable("HistoryTable", "historySchema");
ttb.HasPeriodStart("MyPeriodStart").HasColumnName("PeriodStartColumn");
ttb.HasPeriodEnd("MyPeriodEnd").HasColumnName("PeriodEndColumn");
}));
modelBuilder.Entity<Customer>().ToTable(tb => tb.IsTemporal(ttb =>
{
ttb.UseHistoryTable("ChangedHistoryTable", "changedHistorySchema");
ttb.HasPeriodStart("ChangedMyPeriodStart").HasColumnName("ChangedPeriodStartColumn");
ttb.HasPeriodEnd("ChangedMyPeriodEnd").HasColumnName("ChangedPeriodEndColumn");
}));
modelBuilder.FinalizeModel();
var entity = model.FindEntityType(typeof(Customer));
Assert.True(entity.IsTemporal());
Assert.Equal(5, entity.GetProperties().Count());
Assert.Equal("ChangedHistoryTable", entity.GetHistoryTableName());
Assert.Equal("changedHistorySchema", entity.GetHistoryTableSchema());
var periodStart = entity.GetProperty(entity.GetPeriodStartPropertyName());
var periodEnd = entity.GetProperty(entity.GetPeriodEndPropertyName());
Assert.Equal("ChangedMyPeriodStart", periodStart.Name);
Assert.Equal("ChangedPeriodStartColumn", periodStart[RelationalAnnotationNames.ColumnName]);
Assert.True(periodStart.IsShadowProperty());
Assert.Equal(typeof(DateTime), periodStart.ClrType);
Assert.Equal(ValueGenerated.OnAddOrUpdate, periodStart.ValueGenerated);
Assert.Equal("ChangedMyPeriodEnd", periodEnd.Name);
Assert.Equal("ChangedPeriodEndColumn", periodEnd[RelationalAnnotationNames.ColumnName]);