Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw for ad-hoc type with property that looks like collection navigation #30211

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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