Skip to content

Commit

Permalink
Throw for ad-hoc type with property that looks like collection naviga…
Browse files Browse the repository at this point in the history
…tion

Fixes #30056
  • Loading branch information
ajcvickers committed Feb 4, 2023
1 parent b52ca81 commit cb2015b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/EFCore/Infrastructure/ModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,15 @@ protected virtual void ValidatePropertyMapping(
continue;
}

var isAdHoc = Equals(model.FindAnnotation(CoreAnnotationNames.AdHocModel)?.Value, true);
if (targetType != null)
{
var targetShared = conventionModel.IsShared(targetType);
targetOwned ??= IsOwned(targetType, conventionModel);
// ReSharper disable CheckForReferenceEqualityInstead.1
// ReSharper disable CheckForReferenceEqualityInstead.3
if ((!entityType.IsKeyless
if ((isAdHoc
|| !entityType.IsKeyless
|| targetSequenceType == null)
&& entityType.GetDerivedTypes().All(
dt => dt.GetDeclaredNavigations().FirstOrDefault(n => n.Name == clrProperty.GetSimpleMemberName())
Expand All @@ -237,7 +239,7 @@ protected virtual void ValidatePropertyMapping(
}

throw new InvalidOperationException(
Equals(model.FindAnnotation(CoreAnnotationNames.AdHocModel)?.Value, true)
isAdHoc
? CoreStrings.NavigationNotAddedAdHoc(
entityType.DisplayName(), clrProperty.Name, propertyType.ShortDisplayName())
: CoreStrings.NavigationNotAdded(
Expand All @@ -257,7 +259,7 @@ protected virtual void ValidatePropertyMapping(
else
{
throw new InvalidOperationException(
Equals(model.FindAnnotation(CoreAnnotationNames.AdHocModel)?.Value, true)
isAdHoc
? CoreStrings.PropertyNotAddedAdHoc(
entityType.DisplayName(), clrProperty.Name, propertyType.ShortDisplayName())
: CoreStrings.PropertyNotAdded(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,17 @@ public virtual void Ad_hoc_type_with_reference_navigation_throws()
() => context.Database.SqlQueryRaw<Post>(NormalizeDelimitersInRawString(@"SELECT * FROM [Posts]"))).Message);
}

[ConditionalFact] // Issue #30056
public virtual void Ad_hoc_type_with_collection_navigation_throws()
{
using var context = CreateContext();

Assert.Equal(
CoreStrings.NavigationNotAddedAdHoc("Blog", "Posts", "List<Post>"),
Assert.Throws<InvalidOperationException>(
() => context.Database.SqlQueryRaw<Blog>(NormalizeDelimitersInRawString(@"SELECT * FROM [Blogs]"))).Message);
}

[ConditionalFact]
public virtual void Ad_hoc_type_with_unmapped_property_throws()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public UnmappedCustomer(string customerID)
public bool IsLondon
=> City == "London";

// Unmapped collection navigations are ignored for keyless entity types
[NotMapped]
public virtual List<UnmappedOrder>? Orders { get; set; }

public static UnmappedCustomer FromCustomer(Customer customer)
Expand Down

0 comments on commit cb2015b

Please sign in to comment.