Skip to content

Commit

Permalink
Metadata: Throw error for shared type in inheritance (#21715)
Browse files Browse the repository at this point in the history
Resolves #21570
  • Loading branch information
smitpatel authored Jul 21, 2020
1 parent 7b2fe67 commit 37ecb29
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/EFCore/Infrastructure/ModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,12 @@ private void ValidateClrInheritance(
return;
}

if (entityType.HasSharedClrType
&& entityType.BaseType != null)
{
throw new InvalidOperationException(CoreStrings.SharedTypeDerivedType(entityType.DisplayName()));
}

if (!entityType.HasDefiningNavigation()
&& entityType.FindDeclaredOwnership() == null
&& entityType.BaseType != 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 @@ -1441,4 +1441,7 @@
<data name="TypeNotMarkedAsShared" xml:space="preserve">
<value>Type '{type}' is not been configured as shared type in the model. Before calling 'UsingEntity', please mark the type as shared or add the entity type in the model as shared entity.</value>
</data>
<data name="SharedTypeDerivedType" xml:space="preserve">
<value>The shared type entity type '{entityType}' cannot have a base type.</value>
</data>
</root>
10 changes: 10 additions & 0 deletions test/EFCore.Tests/Infrastructure/ModelValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,16 @@ public virtual void Required_navigation_with_query_filter_on_both_sides_doesnt_i
VerifyLogDoesNotContain(message, modelBuilder.Model);
}

[ConditionalFact]
public virtual void Shared_type_inheritance_throws()
{
var modelBuilder = CreateConventionalModelBuilder();
modelBuilder.SharedEntity<A>("Shared1");
modelBuilder.SharedEntity<C>("Shared2").HasBaseType("Shared1");

VerifyError(CoreStrings.SharedTypeDerivedType("Shared2"), modelBuilder.Model);
}

// INotify interfaces not really implemented; just marking the classes to test metadata construction
private class FullNotificationEntity : INotifyPropertyChanging, INotifyPropertyChanged
{
Expand Down

0 comments on commit 37ecb29

Please sign in to comment.