Skip to content

Commit

Permalink
Fix nested owned types in 2.x snapshots.
Browse files Browse the repository at this point in the history
Fixes #18183
  • Loading branch information
AndriySvyryd committed Oct 23, 2019
1 parent 5a0b029 commit 8b6a593
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/EFCore/Metadata/Builders/OwnedNavigationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
{
Expand All @@ -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<int>("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<int>("OrderId")
.ValueGeneratedOnAdd();
b2.HasKey("OrderId");
b2.ToTable("Order");
b2.HasOne("OrderInfo")
.WithOne("BillingAddress")
.HasForeignKey("Address", "OrderId")
.OnDelete(DeleteBehavior.Cascade);
});
});
});
},
Expand All @@ -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<int>("OrderId")
.ValueGeneratedOnAdd();
b2.HasKey("OrderId");
b2.ToTable("Order");
b2.WithOwner()
.HasForeignKey("OrderId");
});
b1.OwnsOne("Address", "BillingAddress", b2 =>
{
b2.Property<int>("OrderId")
.ValueGeneratedOnAdd();
b2.HasKey("OrderId");
b2.ToTable("Order");
b2.WithOwner()
.HasForeignKey("OrderId");
});
});
});
},
Expand Down

0 comments on commit 8b6a593

Please sign in to comment.