Skip to content

Commit

Permalink
Throw if same name is specified for a different shared type owned ent…
Browse files Browse the repository at this point in the history
…ity type (#22052)

Fixes #21951
  • Loading branch information
AndriySvyryd committed Aug 14, 2020
1 parent 3160094 commit 9b11ad1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3204,6 +3204,18 @@ private InternalForeignKeyBuilder HasOwnership(
return null;
}

if (targetEntityType.IsNamed
&& targetType != null)
{
if (configurationSource == ConfigurationSource.Explicit)
{
throw new InvalidOperationException(CoreStrings.ClashingNamedOwnedType(
targetTypeName, Metadata.DisplayName(), navigation.Name));
}

return null;
}

var newOtherOwnership = otherOwnership.Builder.AddToDeclaringTypeDefinition(configurationSource);
if (newOtherOwnership == null)
{
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore/Properties/CoreStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore/Properties/CoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1495,4 +1495,7 @@
<data name="QueryInvalidMaterializationType" xml:space="preserve">
<value>The query contains a final projection '{projection}' to type '{queryableType}'. Collections in the final projection must be an 'IEnumerable&lt;T&gt;' type such as 'List&lt;T&gt;'. Consider using 'ToList()' or some other mechanism to convert the 'IQueryable&lt;T&gt;' or 'IOrderedEnumerable&lt;T&gt;' into an 'IEnumerable&lt;T&gt;'.</value>
</data>
<data name="ClashingNamedOwnedType" xml:space="preserve">
<value>There is already an entity type named '{ownedTypeName}'. Use a different name when configuring the ownership '{ownerEntityType}.{navigation}'.</value>
</data>
</root>
18 changes: 18 additions & 0 deletions test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,24 @@ public virtual void Shared_type_can_be_used_as_owned_type()
t => { Assert.Equal("Shared4", t.Name); Assert.NotNull(t.FindProperty("NestedLong")); });
}

[ConditionalFact]
public virtual void Shared_type_used_as_owned_type_throws_for_same_name()
{
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<OwnerOfSharedType>(
b =>
{
b.OwnsOne("Shared1", e => e.Reference);
b.OwnsOne("Shared1", e => e.Reference);
Assert.Equal(CoreStrings.ClashingNamedOwnedType(
"Shared1", nameof(OwnerOfSharedType), nameof(OwnerOfSharedType.Collection)),
Assert.Throws<InvalidOperationException>(() =>
b.OwnsMany("Shared1", e => e.Collection)).Message);
});
}

[ConditionalFact]
public virtual void Cannot_add_shared_type_with_same_clr_type_as_weak_entity_type()
{
Expand Down

0 comments on commit 9b11ad1

Please sign in to comment.