From 8b6a59338944cb1bb38a232ebd0e822077dba447 Mon Sep 17 00:00:00 2001 From: AndriySvyryd Date: Wed, 23 Oct 2019 14:17:43 -0700 Subject: [PATCH] Fix nested owned types in 2.x snapshots. Fixes #18183 --- .../Builders/OwnedNavigationBuilder.cs | 15 ++++- .../Internal/MigrationsModelDifferTest.cs | 61 ++++++++++++++++++- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/EFCore/Metadata/Builders/OwnedNavigationBuilder.cs b/src/EFCore/Metadata/Builders/OwnedNavigationBuilder.cs index 4864c3b0c5b..5916448edc7 100644 --- a/src/EFCore/Metadata/Builders/OwnedNavigationBuilder.cs +++ b/src/EFCore/Metadata/Builders/OwnedNavigationBuilder.cs @@ -690,13 +690,24 @@ protected virtual EntityType FindRelatedEntityType([NotNull] string relatedTypeN return relatedEntityType; } + var model = DependentEntityType.Model; if (navigationName != null) { - relatedEntityType = Builder.ModelBuilder.Metadata.FindEntityType(relatedTypeName, navigationName, DependentEntityType); + relatedEntityType = model.FindEntityType(relatedTypeName, navigationName, DependentEntityType); + } + + if (relatedEntityType == null + && model.GetProductVersion().StartsWith("2.", StringComparison.Ordinal)) + { + var owner = DependentEntityType.FindOwnership().PrincipalEntityType; + if (owner.Name == relatedTypeName) + { + relatedEntityType = owner; + } } return relatedEntityType ?? - DependentEntityType.Builder.ModelBuilder.Entity(relatedTypeName, ConfigurationSource.Explicit).Metadata; + Builder.ModelBuilder.Entity(relatedTypeName, ConfigurationSource.Explicit).Metadata; } /// diff --git a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs index 322fbf33c06..c5428420662 100644 --- a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs +++ b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs @@ -7614,6 +7614,7 @@ public void Old_style_ownership_to_new_style() }, source => { + source.HasAnnotation("ProductVersion", "2.2.6-servicing-10079"); source.Entity( "Order", b => { @@ -7627,10 +7628,40 @@ public void Old_style_ownership_to_new_style() b1.ToTable("Order"); - b1.HasOne("Order") + b1.HasOne("Order", "Order") .WithOne("OrderInfo") .HasForeignKey("OrderInfo", "OrderId") .OnDelete(DeleteBehavior.Cascade); + + b1.OwnsOne("Address", "ShippingAddress", b2 => + { + b2.Property("OrderId") + .ValueGeneratedOnAdd(); + + b2.HasKey("OrderId"); + + b2.ToTable("Order"); + + b2.HasOne("OrderInfo") + .WithOne("ShippingAddress") + .HasForeignKey("Address", "OrderId") + .OnDelete(DeleteBehavior.Cascade); + }); + + b1.OwnsOne("Address", "BillingAddress", b2 => + { + b2.Property("OrderId") + .ValueGeneratedOnAdd(); + + b2.HasKey("OrderId"); + + b2.ToTable("Order"); + + b2.HasOne("OrderInfo") + .WithOne("BillingAddress") + .HasForeignKey("Address", "OrderId") + .OnDelete(DeleteBehavior.Cascade); + }); }); }); }, @@ -7649,8 +7680,34 @@ public void Old_style_ownership_to_new_style() b1.ToTable("Order"); - b1.WithOwner() + b1.WithOwner("Order") .HasForeignKey("OrderId"); + + b1.OwnsOne("Address", "ShippingAddress", b2 => + { + b2.Property("OrderId") + .ValueGeneratedOnAdd(); + + b2.HasKey("OrderId"); + + b2.ToTable("Order"); + + b2.WithOwner() + .HasForeignKey("OrderId"); + }); + + b1.OwnsOne("Address", "BillingAddress", b2 => + { + b2.Property("OrderId") + .ValueGeneratedOnAdd(); + + b2.HasKey("OrderId"); + + b2.ToTable("Order"); + + b2.WithOwner() + .HasForeignKey("OrderId"); + }); }); }); },