diff --git a/src/EFCore/Metadata/Conventions/InversePropertyAttributeConvention.cs b/src/EFCore/Metadata/Conventions/InversePropertyAttributeConvention.cs index f9950b1571f..a226edf1da2 100644 --- a/src/EFCore/Metadata/Conventions/InversePropertyAttributeConvention.cs +++ b/src/EFCore/Metadata/Conventions/InversePropertyAttributeConvention.cs @@ -186,12 +186,12 @@ private IConventionRelationshipBuilder ConfigureInverseNavigation( var ownership = entityType.FindOwnership(); if (ownership != null && ownership.PrincipalEntityType == targetEntityTypeBuilder.Metadata - && ownership.PrincipalToDependent.GetIdentifyingMemberInfo() != inverseNavigationPropertyInfo) + && ownership.PrincipalToDependent?.GetIdentifyingMemberInfo() != inverseNavigationPropertyInfo) { Dependencies.Logger.NonOwnershipInverseNavigationWarning( entityType, navigationMemberInfo, targetEntityTypeBuilder.Metadata, inverseNavigationPropertyInfo, - ownership.PrincipalToDependent.GetIdentifyingMemberInfo()); + ownership.PrincipalToDependent?.GetIdentifyingMemberInfo()); return null; } diff --git a/src/EFCore/Metadata/Conventions/RelationshipDiscoveryConvention.cs b/src/EFCore/Metadata/Conventions/RelationshipDiscoveryConvention.cs index 0535c8dc0f8..69f4b5aa040 100644 --- a/src/EFCore/Metadata/Conventions/RelationshipDiscoveryConvention.cs +++ b/src/EFCore/Metadata/Conventions/RelationshipDiscoveryConvention.cs @@ -120,7 +120,7 @@ private IReadOnlyList FindRelationshipCandidates(IConvent var targetOwnership = candidateTargetEntityType.FindOwnership(); if (targetOwnership != null && (targetOwnership.PrincipalEntityType != entityType - || targetOwnership.PrincipalToDependent.Name != navigationPropertyInfo.GetSimpleMemberName()) + || targetOwnership.PrincipalToDependent?.Name != navigationPropertyInfo.GetSimpleMemberName()) && (ownership == null || ownership.PrincipalEntityType != candidateTargetEntityType)) { @@ -163,7 +163,7 @@ private IReadOnlyList FindRelationshipCandidates(IConvent && (candidateTargetEntityType.IsOwned() || model.FindIsOwnedConfigurationSource(targetClrType) == null) && (ownership.PrincipalEntityType != candidateTargetEntityType - || ownership.PrincipalToDependent.Name != inversePropertyInfo.GetSimpleMemberName())) + || ownership.PrincipalToDependent?.Name != inversePropertyInfo.GetSimpleMemberName())) || (entityType.HasDefiningNavigation() && !candidateTargetEntityType.IsInDefinitionPath(entityType.ClrType) && (entityType.DefiningEntityType != candidateTargetEntityType diff --git a/src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs b/src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs index 12554cce8fd..e97bba82cf6 100644 --- a/src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs +++ b/src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs @@ -2788,7 +2788,7 @@ public virtual InternalEntityTypeBuilder GetTargetEntityTypeBuilder( if (existingOwnership != null && entityType.Model.IsOwned(targetClrType) && (existingOwnership.PrincipalEntityType != entityType - || existingOwnership.PrincipalToDependent.Name != navigationInfo.GetSimpleMemberName())) + || existingOwnership.PrincipalToDependent?.Name != navigationInfo.GetSimpleMemberName())) { return configurationSource.HasValue && !targetClrType.Equals(Metadata.ClrType) diff --git a/test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs b/test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs index b39d3ff0daf..c6a219c865b 100644 --- a/test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs +++ b/test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs @@ -20,7 +20,6 @@ public abstract class OwnedTypesTestBase : ModelBuilderTestBase public virtual void Can_configure_owned_type() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Entity() .OwnsOne( @@ -32,13 +31,14 @@ public virtual void Can_configure_owned_type() db.HasIndex(d => d.CustomerId); }); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); var owner = model.FindEntityType(typeof(Customer)); Assert.Equal(typeof(Customer).FullName, owner.Name); var ownership = owner.FindNavigation(nameof(Customer.Details)).ForeignKey; Assert.True(ownership.IsOwnership); Assert.Equal(nameof(Customer.Details), ownership.PrincipalToDependent.Name); + Assert.Equal(nameof(CustomerDetails.Customer), ownership.DependentToPrincipal.Name); Assert.Equal("CustomerAlternateKey", ownership.Properties.Single().Name); Assert.Equal(nameof(Customer.AlternateKey), ownership.PrincipalKey.Properties.Single().Name); var owned = ownership.DeclaringEntityType; @@ -55,7 +55,6 @@ public virtual void Can_configure_owned_type() public virtual void Can_configure_owned_type_using_nested_closure() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Entity().OwnsOne( c => c.Details, @@ -63,6 +62,8 @@ public virtual void Can_configure_owned_type_using_nested_closure() .WithOwner(d => d.Customer) .HasAnnotation("bar", "foo")); + var model = modelBuilder.FinalizeModel(); + var ownership = model.FindEntityType(typeof(Customer)).FindNavigation(nameof(Customer.Details)).ForeignKey; var owned = ownership.DeclaringEntityType; Assert.True(ownership.IsOwnership); @@ -74,7 +75,7 @@ public virtual void Can_configure_owned_type_using_nested_closure() public virtual void Can_configure_owned_type_inverse() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; + IModel model = modelBuilder.Model; modelBuilder.Entity().OwnsOne(c => c.Details); @@ -85,7 +86,7 @@ public virtual void Can_configure_owned_type_inverse() modelBuilder.Entity().OwnsOne(c => c.Details) .HasOne(d => d.Customer); - modelBuilder.FinalizeModel(); + model = modelBuilder.FinalizeModel(); var ownership = owner.FindNavigation(nameof(Customer.Details)).ForeignKey; Assert.True(ownership.IsOwnership); @@ -99,7 +100,6 @@ public virtual void Can_configure_owned_type_inverse() public virtual void Can_configure_owned_type_properties() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Entity().OwnsOne(c => c.Details) .UsePropertyAccessMode(PropertyAccessMode.FieldDuringConstruction) @@ -107,7 +107,7 @@ public virtual void Can_configure_owned_type_properties() .Ignore(d => d.Id) .Property("foo"); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); var owner = model.FindEntityType(typeof(Customer)); var owned = owner.FindNavigation(nameof(Customer.Details)).ForeignKey.DeclaringEntityType; @@ -121,12 +121,11 @@ public virtual void Can_configure_owned_type_properties() public virtual void Can_configure_owned_type_key() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Entity().OwnsOne(c => c.Details) .HasKey(c => c.Id); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); var owner = model.FindEntityType(typeof(Customer)); var owned = owner.FindNavigation(nameof(Customer.Details)).ForeignKey.DeclaringEntityType; @@ -140,14 +139,13 @@ public virtual void Can_configure_owned_type_key() public virtual void Can_configure_ownership_foreign_key() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Entity() .OwnsOne(c => c.Details) .WithOwner(d => d.Customer) .HasForeignKey(c => c.Id); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); var ownership = model.FindEntityType(typeof(Customer)).FindNavigation(nameof(Customer.Details)).ForeignKey; Assert.Equal(nameof(CustomerDetails.Id), ownership.Properties.Single().Name); @@ -155,17 +153,67 @@ public virtual void Can_configure_ownership_foreign_key() Assert.Single(ownership.DeclaringEntityType.GetForeignKeys()); } + [ConditionalFact] + public virtual void Can_configure_another_relationship_to_owner() + { + var modelBuilder = CreateModelBuilder(); + + modelBuilder.Entity().OwnsOne( + c => c.Details, + r => + { + r.WithOwner(); + r.HasOne(d => d.Customer) + .WithMany(); + }); + + var model = modelBuilder.FinalizeModel(); + + var ownership = model.FindEntityType(typeof(Customer)).FindNavigation(nameof(Customer.Details)).ForeignKey; + var owned = ownership.DeclaringEntityType; + Assert.True(ownership.IsOwnership); + Assert.Equal(nameof(Customer.Details), ownership.PrincipalToDependent.Name); + Assert.Null(ownership.DependentToPrincipal); + Assert.Equal("CustomerId", ownership.Properties.Single().Name); + + var otherFk = owned.GetForeignKeys().Single(fk => fk != ownership); + Assert.Null(otherFk.PrincipalToDependent); + Assert.Equal(nameof(CustomerDetails.Customer), otherFk.DependentToPrincipal.Name); + Assert.Equal("CustomerId1", otherFk.Properties.Single().Name); + Assert.False(otherFk.IsOwnership); + Assert.False(otherFk.IsUnique); + + Assert.Equal(1, model.GetEntityTypes().Count(e => e.ClrType == typeof(CustomerDetails))); + } + + [ConditionalFact] + public virtual void Changing_ownership_uniqueness_throws() + { + var modelBuilder = CreateModelBuilder(); + + modelBuilder.Entity().OwnsOne( + c => c.Details, + r => + { + r.HasOne(d => d.Customer) + .WithMany(); + }); + + + Assert.Equal(CoreStrings.NavigationNotAdded(nameof(Customer), nameof(Customer.Details), nameof(CustomerDetails)), + Assert.Throws(() => modelBuilder.FinalizeModel()).Message); + } + [ConditionalFact] public virtual void Can_configure_multiple_ownerships() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Ignore(); modelBuilder.Entity().OwnsOne(c => c.Details); modelBuilder.Entity().OwnsOne(c => c.Details); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); var ownership1 = model.FindEntityType(typeof(OtherCustomer)).FindNavigation(nameof(Customer.Details)).ForeignKey; var ownership2 = model.FindEntityType(typeof(SpecialCustomer)).FindNavigation(nameof(Customer.Details)).ForeignKey; @@ -181,7 +229,6 @@ public virtual void Can_configure_multiple_ownerships() public virtual void Can_configure_one_to_one_relationship_from_an_owned_type() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Ignore(); modelBuilder.Entity(); @@ -190,11 +237,11 @@ public virtual void Can_configure_one_to_one_relationship_from_an_owned_type() .WithOne() .HasPrincipalKey(); - Assert.NotNull(model.FindEntityType(typeof(CustomerDetails))); + Assert.NotNull(modelBuilder.Model.FindEntityType(typeof(CustomerDetails))); modelBuilder.Entity().OwnsOne(c => c.Details); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); var ownership = model.FindEntityType(typeof(OtherCustomer)).FindNavigation(nameof(Customer.Details)).ForeignKey; var foreignKey = model.FindEntityType(typeof(SpecialCustomer)).GetReferencingForeignKeys() @@ -211,7 +258,7 @@ public virtual void Can_configure_one_to_one_relationship_from_an_owned_type() public virtual void Can_configure_owned_type_collection_from_an_owned_type() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; + IModel model = modelBuilder.Model; modelBuilder.Ignore(); var entityBuilder = modelBuilder.Entity().OwnsOne(o => o.Customer) @@ -227,7 +274,7 @@ public virtual void Can_configure_owned_type_collection_from_an_owned_type() entityBuilder.HasKey(o => o.OrderId); - modelBuilder.FinalizeModel(); + model = modelBuilder.FinalizeModel(); Assert.True(ownership.IsOwnership); Assert.True(ownership.IsUnique); @@ -251,7 +298,6 @@ public virtual void Can_configure_owned_type_collection_from_an_owned_type() public virtual void Can_configure_owned_type_collection() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; var entityBuilder = modelBuilder.Entity().OwnsMany(c => c.Orders) .UsePropertyAccessMode(PropertyAccessMode.FieldDuringConstruction) @@ -265,7 +311,7 @@ public virtual void Can_configure_owned_type_collection() entityBuilder.WithOwner(o => o.Customer) .HasPrincipalKey(c => c.AlternateKey); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); var owner = model.FindEntityType(typeof(Customer)); var ownership = owner.FindNavigation(nameof(Customer.Orders)).ForeignKey; @@ -297,7 +343,6 @@ public virtual void Can_configure_owned_type_collection() public virtual void Can_configure_owned_type_collection_using_nested_closure() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Entity().OwnsMany( c => c.Orders, @@ -315,7 +360,7 @@ public virtual void Can_configure_owned_type_collection_using_nested_closure() .HasForeignKey("DifferentCustomerId"); }); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); var ownership = model.FindEntityType(typeof(Customer)).FindNavigation(nameof(Customer.Orders)).ForeignKey; var owned = ownership.DeclaringEntityType; @@ -335,7 +380,6 @@ public virtual void Can_configure_owned_type_collection_using_nested_closure() public virtual void Can_configure_one_to_one_relationship_from_an_owned_type_collection() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Ignore(); modelBuilder.Ignore(); @@ -348,12 +392,12 @@ public virtual void Can_configure_one_to_one_relationship_from_an_owned_type_col .HasPrincipalKey(); }); - Assert.NotNull(model.FindEntityType(typeof(Order))); + Assert.NotNull(modelBuilder.Model.FindEntityType(typeof(Order))); modelBuilder.Entity().OwnsMany(c => c.Orders) .HasKey(o => o.OrderId); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); Assert.Null(model.FindEntityType(typeof(Order))); var ownership1 = model.FindEntityType(typeof(OtherCustomer)).FindNavigation(nameof(Customer.Orders)).ForeignKey; @@ -387,7 +431,6 @@ public virtual void Can_configure_one_to_one_relationship_from_an_owned_type_col public virtual void Can_configure_owned_type_from_an_owned_type_collection() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Ignore(); modelBuilder.Entity().OwnsMany( @@ -399,7 +442,7 @@ public virtual void Can_configure_owned_type_from_an_owned_type_collection() new OrderDetails { OrderId = -1 }); }); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); var ownership = model.FindEntityType(typeof(Customer)).FindNavigation(nameof(Customer.Orders)).ForeignKey; var owned = ownership.DeclaringEntityType; @@ -422,7 +465,6 @@ public virtual void Can_configure_owned_type_from_an_owned_type_collection() public virtual void Can_chain_owned_type_collection_configurations() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Ignore(); modelBuilder.Entity().OwnsMany( @@ -435,7 +477,7 @@ public virtual void Can_chain_owned_type_collection_configurations() .HasKey(p => p.Id); }); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); var ownership = model.FindEntityType(typeof(Customer)).FindNavigation(nameof(Customer.Orders)).ForeignKey; var owned = ownership.DeclaringEntityType; @@ -461,7 +503,6 @@ public virtual void Can_chain_owned_type_collection_configurations() public virtual void Can_configure_owned_type_collection_without_explicit_key() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Entity().OwnsMany( c => c.Orders, @@ -472,7 +513,7 @@ public virtual void Can_configure_owned_type_collection_without_explicit_key() r.OwnsMany(o => o.Products); }); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); var ownership = model.FindEntityType(typeof(Customer)).FindNavigation(nameof(Customer.Orders)).ForeignKey; var owned = ownership.DeclaringEntityType; @@ -503,7 +544,6 @@ public virtual void Can_configure_owned_type_collection_without_explicit_key() public virtual void Can_configure_owned_type_collection_without_explicit_key_or_candidate() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Entity().OwnsMany( c => c.Orders, @@ -516,7 +556,7 @@ public virtual void Can_configure_owned_type_collection_without_explicit_key_or_ .Ignore(p => p.Id); }); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); var ownership = model.FindEntityType(typeof(Customer)).FindNavigation(nameof(Customer.Orders)).ForeignKey; var owned = ownership.DeclaringEntityType; @@ -578,9 +618,8 @@ public virtual void Can_configure_owned_type_collection_with_one_call() so.OwnsOne(o => o.BackOrder); }).Metadata; - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); - var model = (Model)modelBuilder.Model; var customer = model.FindEntityType(typeof(Customer)); var ownership = customer.FindNavigation(nameof(Customer.Orders)).ForeignKey; @@ -598,10 +637,11 @@ public virtual void Can_configure_owned_type_collection_with_one_call() Assert.Equal(7, modelBuilder.Model.GetEntityTypes().Count(e => !e.HasDefiningNavigation())); Assert.Equal(5, modelBuilder.Model.GetEntityTypes().Count(e => e.IsOwned())); - Assert.Null(model.FindIgnoredConfigurationSource(typeof(Order))); - Assert.Null(model.FindIgnoredConfigurationSource(typeof(SpecialOrder))); - Assert.Null(model.FindIgnoredConfigurationSource(typeof(Customer))); - Assert.Null(model.FindIgnoredConfigurationSource(typeof(SpecialCustomer))); + var conventionModel = (IConventionModel)modelBuilder.Model; + Assert.Null(conventionModel.FindIgnoredConfigurationSource(typeof(Order))); + Assert.Null(conventionModel.FindIgnoredConfigurationSource(typeof(SpecialOrder))); + Assert.Null(conventionModel.FindIgnoredConfigurationSource(typeof(Customer))); + Assert.Null(conventionModel.FindIgnoredConfigurationSource(typeof(SpecialCustomer))); } [ConditionalFact] @@ -626,9 +666,8 @@ public virtual void Can_configure_owned_type_collection_with_one_call_afterwards .OwnsMany(c => c.Orders) .HasKey(o => o.OrderId); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); - var model = (Model)modelBuilder.Model; var customer = model.FindEntityType(typeof(Customer)); var ownership = customer.FindNavigation(nameof(Customer.Orders)).ForeignKey; @@ -646,21 +685,21 @@ public virtual void Can_configure_owned_type_collection_with_one_call_afterwards Assert.Equal(7, modelBuilder.Model.GetEntityTypes().Count(e => !e.HasDefiningNavigation())); Assert.Equal(5, modelBuilder.Model.GetEntityTypes().Count(e => e.IsOwned())); - Assert.Null(model.FindIgnoredConfigurationSource(typeof(Order))); - Assert.Null(model.FindIgnoredConfigurationSource(typeof(SpecialOrder))); - Assert.Null(model.FindIgnoredConfigurationSource(typeof(Customer))); - Assert.Null(model.FindIgnoredConfigurationSource(typeof(SpecialCustomer))); + var conventionModel = (IConventionModel)modelBuilder.Model; + Assert.Null(conventionModel.FindIgnoredConfigurationSource(typeof(Order))); + Assert.Null(conventionModel.FindIgnoredConfigurationSource(typeof(SpecialOrder))); + Assert.Null(conventionModel.FindIgnoredConfigurationSource(typeof(Customer))); + Assert.Null(conventionModel.FindIgnoredConfigurationSource(typeof(SpecialCustomer))); } [ConditionalFact] public virtual void Can_configure_single_owned_type_using_attribute() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Entity(); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); var owner = model.FindEntityType(typeof(SpecialOrder)); var ownership = owner.FindNavigation(nameof(SpecialOrder.ShippingAddress)).ForeignKey; @@ -672,7 +711,6 @@ public virtual void Can_configure_single_owned_type_using_attribute() public virtual void Can_configure_fk_on_multiple_ownerships() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Ignore(); modelBuilder.Ignore(); @@ -690,6 +728,8 @@ public virtual void Can_configure_fk_on_multiple_ownerships() .WithOwner() .HasForeignKey("BookLabelId"); + IModel model = modelBuilder.Model; + var bookOwnership1 = model.FindEntityType(typeof(Book)).FindNavigation(nameof(Book.Label)).ForeignKey; var bookOwnership2 = model.FindEntityType(typeof(Book)).FindNavigation(nameof(Book.AlternateLabel)).ForeignKey; Assert.NotSame(bookOwnership1.DeclaringEntityType, bookOwnership2.DeclaringEntityType); @@ -703,21 +743,20 @@ public virtual void Can_configure_fk_on_multiple_ownerships() modelBuilder.Entity().OwnsOne(b => b.Label).Ignore(l => l.Book); modelBuilder.Entity().OwnsOne(b => b.AlternateLabel).Ignore(l => l.Book); - modelBuilder.FinalizeModel(); + modelBuilder.FinalizeModel(); } [ConditionalFact] public virtual void Can_map_base_of_owned_type() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Ignore(); modelBuilder.Entity().OwnsOne(c => c.Details); modelBuilder.Entity(); modelBuilder.Ignore(); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); Assert.NotNull(model.FindEntityType(typeof(BookDetailsBase))); var owner = model.FindEntityType(typeof(Customer)); @@ -732,14 +771,13 @@ public virtual void Can_map_base_of_owned_type() public virtual void Can_map_base_of_owned_type_first() { var modelBuilder = CreateModelBuilder(); - var model = modelBuilder.Model; modelBuilder.Ignore(); modelBuilder.Entity(); modelBuilder.Entity().OwnsOne(c => c.Details); modelBuilder.Ignore(); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); Assert.NotNull(model.FindEntityType(typeof(BookDetailsBase))); var owner = model.FindEntityType(typeof(Customer)); @@ -754,12 +792,13 @@ public virtual void Can_map_base_of_owned_type_first() public virtual void Can_map_derived_of_owned_type() { var modelBuilder = CreateModelBuilder(); - var model = (Model)modelBuilder.Model; modelBuilder.Ignore(); modelBuilder.Entity().OwnsOne(c => c.Details); modelBuilder.Entity(); + IModel model = modelBuilder.Model; + var owner = model.FindEntityType(typeof(OrderCombination)); var owned = owner.FindNavigation(nameof(OrderCombination.Details)).ForeignKey.DeclaringEntityType; Assert.NotEmpty(owned.GetDirectlyDerivedTypes()); @@ -776,17 +815,17 @@ public virtual void Can_map_derived_of_owned_type() modelBuilder.Entity().Ignore(c => c.Details); modelBuilder.Entity().Ignore(c => c.Details); - modelBuilder.FinalizeModel(); } [ConditionalFact] public virtual void Can_map_derived_of_owned_type_first() { var modelBuilder = CreateModelBuilder(); - var model = (Model)modelBuilder.Model; modelBuilder.Entity().OwnsOne(c => c.Details); + IModel model = modelBuilder.Model; + var owner = model.FindEntityType(typeof(OrderCombination)); var owned = owner.FindNavigation(nameof(OrderCombination.Details)).ForeignKey.DeclaringEntityType; Assert.NotEmpty(owned.GetDirectlyDerivedTypes()); @@ -803,6 +842,7 @@ public virtual void Can_map_derived_of_owned_type_first() modelBuilder.Entity().Ignore(c => c.Details); modelBuilder.Entity().Ignore(c => c.Details); + modelBuilder.FinalizeModel(); } @@ -866,9 +906,9 @@ public virtual void Can_configure_chained_ownerships() }); }); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); - VerifyOwnedBookLabelModel(modelBuilder.Model); + VerifyOwnedBookLabelModel(model); } [ConditionalFact] @@ -922,9 +962,9 @@ public virtual void Can_configure_chained_ownerships_different_order() }); }); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); - VerifyOwnedBookLabelModel(modelBuilder.Model); + VerifyOwnedBookLabelModel(model); } [ConditionalFact] @@ -970,9 +1010,9 @@ public virtual void Can_configure_hierarchy_with_reference_navigations_as_owned( .Ignore(l => l.Book); }); - VerifyOwnedBookLabelModel(modelBuilder.Model); + var model = modelBuilder.FinalizeModel(); - modelBuilder.FinalizeModel(); + VerifyOwnedBookLabelModel(model); } [ConditionalFact] @@ -1017,12 +1057,12 @@ public virtual void Can_configure_hierarchy_with_reference_navigations_as_owned_ .Ignore(l => l.Book); }); - VerifyOwnedBookLabelModel(modelBuilder.Model); + var model = modelBuilder.FinalizeModel(); - modelBuilder.FinalizeModel(); + VerifyOwnedBookLabelModel(model); } - protected virtual void VerifyOwnedBookLabelModel(IMutableModel model) + protected virtual void VerifyOwnedBookLabelModel(IModel model) { var bookOwnership1 = model.FindEntityType(typeof(Book)).FindNavigation(nameof(Book.Label)).ForeignKey; var bookOwnership2 = model.FindEntityType(typeof(Book)).FindNavigation(nameof(Book.AlternateLabel)).ForeignKey; @@ -1085,7 +1125,7 @@ public virtual void Can_configure_self_ownership() modelBuilder.Ignore(); modelBuilder.Entity().OwnsOne(l => l.AnotherBookLabel, ab => ab.OwnsOne(l => l.AnotherBookLabel)); - var model = modelBuilder.Model; + var model = modelBuilder.FinalizeModel(); var bookLabelOwnership = model.FindEntityType(typeof(BookLabel)).FindNavigation(nameof(BookLabel.AnotherBookLabel)) .ForeignKey; @@ -1095,8 +1135,6 @@ public virtual void Can_configure_self_ownership() Assert.True(selfOwnership.IsOwnership); Assert.Equal(1, model.GetEntityTypes().Count(e => e.ClrType == typeof(BookLabel))); Assert.Equal(2, model.GetEntityTypes().Count(e => e.ClrType == typeof(AnotherBookLabel))); - - modelBuilder.FinalizeModel(); } [ConditionalFact] @@ -1125,8 +1163,7 @@ public virtual void Reconfiguring_owned_type_as_non_owned_throws() Assert.Equal( CoreStrings.ClashingOwnedEntityType(nameof(CustomerDetails)), Assert.Throws( - () => - modelBuilder.Entity().HasOne(c => c.Details)).Message); + () => modelBuilder.Entity().HasOne(c => c.Details)).Message); } [ConditionalFact] @@ -1204,7 +1241,9 @@ public virtual void OwnedType_can_derive_from_Collection() var modelBuilder = CreateModelBuilder(); modelBuilder.Entity().OwnsOne(o => o.InverseNav); - Assert.Single(modelBuilder.Model.GetEntityTypes(typeof(List))); + var model = modelBuilder.FinalizeModel(); + + Assert.Single(model.GetEntityTypes(typeof(List))); } [ConditionalFact] @@ -1221,7 +1260,9 @@ public virtual void Weak_types_with_FK_to_another_entity_works() e => e.Bill2, o => o.HasOne().WithMany().HasPrincipalKey(c => c.Name).HasForeignKey(d => d.Country)); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); + + Assert.Equal(4, model.GetEntityTypes().Count()); } [ConditionalFact] @@ -1231,9 +1272,9 @@ public virtual void Inheritance_where_base_has_multiple_owned_types_works() modelBuilder.Entity(); modelBuilder.Entity(); - modelBuilder.FinalizeModel(); + var model = modelBuilder.FinalizeModel(); - Assert.Equal(4, modelBuilder.Model.GetEntityTypes().Count()); + Assert.Equal(4, model.GetEntityTypes().Count()); } } }