Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/EFCore/EFCore.baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -3845,7 +3845,10 @@
"Member": "static string ConflictingKeylessAndPrimaryKeyAttributes(object? entity);"
},
{
"Member": "static string ConflictingPropertyOrNavigation(object? member, object? type, object? conflictingType);"
"Member": "static string ConflictingPropertyOrNavigationOnBaseType(object? member, object? type, object? conflictingMemberKind, object? conflictingType);"
},
{
"Member": "static string ConflictingPropertyOrNavigationWithKind(object? member, object? type, object? conflictingMemberKind);"
},
{
"Member": "static string ConflictingRelationshipConversions(object? entityType, object? property, object? valueConversion, object? conflictingValueConversion);"
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore/Metadata/Builders/ReferenceNavigationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ private InternalForeignKeyBuilder WithOneBuilder(MemberIdentity reference)
&& ReferenceName == referenceName)
{
throw new InvalidOperationException(
CoreStrings.ConflictingPropertyOrNavigation(
referenceName, RelatedEntityType.DisplayName(), RelatedEntityType.DisplayName()));
CoreStrings.ConflictingPropertyOrNavigationWithKind(
referenceName, RelatedEntityType.DisplayName(), "navigation"));
}

var pointsToPrincipal = !foreignKey.IsSelfReferencing()
Expand Down
13 changes: 4 additions & 9 deletions src/EFCore/Metadata/Internal/EntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1451,16 +1451,14 @@ public virtual Navigation AddNavigation(MemberIdentity navigationMember, Foreign
}

throw new InvalidOperationException(
CoreStrings.ConflictingPropertyOrNavigation(
name, DisplayName(), duplicateNavigation.DeclaringEntityType.DisplayName()));
duplicateNavigation.FormatConflictingMemberMessage(name, this));
}

var duplicateProperty = FindMembersInHierarchy(name).FirstOrDefault();
if (duplicateProperty != null)
{
throw new InvalidOperationException(
CoreStrings.ConflictingPropertyOrNavigation(
name, DisplayName(), ((IReadOnlyTypeBase)duplicateProperty.DeclaringType).DisplayName()));
duplicateProperty.FormatConflictingMemberMessage(name, this));
}

Check.DebugAssert(
Expand Down Expand Up @@ -1633,8 +1631,7 @@ public virtual IEnumerable<Navigation> GetNavigations()
if (duplicateProperty != null)
{
throw new InvalidOperationException(
CoreStrings.ConflictingPropertyOrNavigation(
name, DisplayName(), duplicateProperty.DeclaringType.DisplayName()));
duplicateProperty.FormatConflictingMemberMessage(name, this));
}

if (memberInfo != null)
Expand Down Expand Up @@ -2286,9 +2283,7 @@ public virtual ServiceProperty AddServiceProperty(
if (duplicateMember != null)
{
throw new InvalidOperationException(
CoreStrings.ConflictingPropertyOrNavigation(
name, DisplayName(),
((IReadOnlyTypeBase)duplicateMember.DeclaringType).DisplayName()));
duplicateMember.FormatConflictingMemberMessage(name, this));
}

ValidateClrMember(name, memberInfo, false);
Expand Down
3 changes: 1 addition & 2 deletions src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,7 @@ public override void RemoveMembersInHierarchy(string propertyName, Configuration
if (conflictingNavigation.GetConfigurationSource() == ConfigurationSource.Explicit)
{
throw new InvalidOperationException(
CoreStrings.ConflictingPropertyOrNavigation(
propertyName, Metadata.DisplayName(), conflictingNavigation.DeclaringEntityType.DisplayName()));
conflictingNavigation.FormatConflictingMemberMessage(propertyName, Metadata));
}

var foreignKey = conflictingNavigation.ForeignKey;
Expand Down
50 changes: 50 additions & 0 deletions src/EFCore/Metadata/Internal/PropertyBaseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,56 @@ or PropertyAccessMode.FieldDuringConstruction
return false;
}

/// <summary>
/// Builds the message for the diagnostic that fires when a member conflicts with an existing
/// member on the structural type or one of its base types. The kind of the conflicting member
/// is humanized via <see cref="GetMemberKindString" /> so the user-facing message uses stable
/// labels like "property", "complex property", "navigation", "skip navigation", or
/// "service property" regardless of whether the conflicting member came from a model or a
/// runtime model.
/// </summary>
/// <remarks>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </remarks>
public static string FormatConflictingMemberMessage(
this IReadOnlyPropertyBase conflictingMember,
string newMemberName,
IReadOnlyTypeBase owningType)
{
var conflictingMemberKind = GetMemberKindString(conflictingMember);
var owningTypeDisplayName = owningType.DisplayName();

// Compare the actual metadata instances rather than display names to avoid false positives when
// two distinct types share a simple name (e.g. same name in different namespaces or hierarchies).
return conflictingMember.DeclaringType == owningType
? CoreStrings.ConflictingPropertyOrNavigationWithKind(newMemberName, owningTypeDisplayName, conflictingMemberKind)
: CoreStrings.ConflictingPropertyOrNavigationOnBaseType(
newMemberName,
owningTypeDisplayName,
conflictingMemberKind,
((IReadOnlyTypeBase)conflictingMember.DeclaringType).DisplayName());
}

/// <summary>
/// Returns a human-readable label for the kind of the given member (e.g. "property",
/// "complex property", "navigation", "skip navigation", "service property"). Used to build
/// user-facing diagnostic messages without coupling the message text to internal CLR class
/// names (such as <c>RuntimeProperty</c> or <c>SkipNavigation</c>).
/// </summary>
private static string GetMemberKindString(IReadOnlyPropertyBase member)
=> member switch
{
IReadOnlyComplexProperty => "complex property",
IReadOnlySkipNavigation => "skip navigation",
IReadOnlyNavigation => "navigation",
IReadOnlyServiceProperty => "service property",
IReadOnlyProperty => "property",
_ => member.GetType().Name
};

private static string GetNoFieldErrorMessage(IPropertyBase propertyBase)
=> propertyBase.DeclaringType switch
{
Expand Down
8 changes: 2 additions & 6 deletions src/EFCore/Metadata/Internal/TypeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -634,9 +634,7 @@ private void CheckDiscriminatorProperty(Property? property)
if (conflictingMember != null)
{
throw new InvalidOperationException(
CoreStrings.ConflictingPropertyOrNavigation(
name, DisplayName(),
((IReadOnlyTypeBase)conflictingMember.DeclaringType).DisplayName()));
conflictingMember.FormatConflictingMemberMessage(name, this));
}

if (memberInfo != null)
Expand Down Expand Up @@ -1082,9 +1080,7 @@ public virtual IReadOnlyDictionary<string, FieldInfo> GetRuntimeFields()
if (conflictingMember != null)
{
throw new InvalidOperationException(
CoreStrings.ConflictingPropertyOrNavigation(
name, DisplayName(),
conflictingMember.DeclaringType.DisplayName()));
conflictingMember.FormatConflictingMemberMessage(name, this));
}

if (memberInfo != null)
Expand Down
16 changes: 12 additions & 4 deletions src/EFCore/Properties/CoreStrings.Designer.cs

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

7 changes: 5 additions & 2 deletions src/EFCore/Properties/CoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,11 @@
<data name="ConflictingKeylessAndPrimaryKeyAttributes" xml:space="preserve">
<value>The entity type '{entity}' has both [Keyless] and [PrimaryKey] attributes; one must be removed.</value>
</data>
<data name="ConflictingPropertyOrNavigation" xml:space="preserve">
<value>The property or navigation '{member}' cannot be added to the '{type}' type because a property or navigation with the same name already exists on the '{conflictingType}' type.</value>
<data name="ConflictingPropertyOrNavigationOnBaseType" xml:space="preserve">
<value>The member '{member}' cannot be added to the '{type}' type because a {conflictingMemberKind} with the same name already exists on the '{conflictingType}' type. Remove the existing {conflictingMemberKind} first.</value>
</data>
<data name="ConflictingPropertyOrNavigationWithKind" xml:space="preserve">
<value>The member '{member}' cannot be added to the '{type}' type because a {conflictingMemberKind} with the same name already exists. Remove the existing {conflictingMemberKind} first.</value>
</data>
<data name="ConflictingRelationshipConversions" xml:space="preserve">
<value>The property '{entityType}.{property}' participates in several relationship chains that have conflicting conversions: '{valueConversion}' and '{conflictingValueConversion}'.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3050,7 +3050,7 @@ public virtual void Throws_on_duplicate_navigation_when_self_referencing()
var modelBuilder = CreateModelBuilder();

Assert.Equal(
CoreStrings.ConflictingPropertyOrNavigation("SelfRef1", nameof(SelfRef), nameof(SelfRef)),
CoreStrings.ConflictingPropertyOrNavigationWithKind("SelfRef1", nameof(SelfRef), "navigation"),
Assert.Throws<InvalidOperationException>(()
=> modelBuilder.Entity<SelfRef>().HasOne(e => e.SelfRef1).WithOne(e => e.SelfRef1)).Message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public void Adding_property_throws_when_parent_type_has_property_with_same_name(
b.BaseType = a;

Assert.Equal(
CoreStrings.ConflictingPropertyOrNavigation("G", typeof(B).Name, typeof(A).Name),
CoreStrings.ConflictingPropertyOrNavigationOnBaseType("G", typeof(B).Name, "property", typeof(A).Name),
Assert.Throws<InvalidOperationException>(() => b.AddProperty("G")).Message);
}

Expand All @@ -389,7 +389,7 @@ public void Adding_property_throws_when_grandparent_type_has_property_with_same_
d.BaseType = c;

Assert.Equal(
CoreStrings.ConflictingPropertyOrNavigation("G", typeof(D).Name, typeof(A).Name),
CoreStrings.ConflictingPropertyOrNavigationOnBaseType("G", typeof(D).Name, "property", typeof(A).Name),
Assert.Throws<InvalidOperationException>(() => d.AddProperty("G")).Message);
}

Expand All @@ -406,7 +406,7 @@ public void Adding_property_throws_when_child_type_has_property_with_same_name()
b.AddProperty(A.GProperty);

Assert.Equal(
CoreStrings.ConflictingPropertyOrNavigation("G", typeof(A).Name, typeof(B).Name),
CoreStrings.ConflictingPropertyOrNavigationOnBaseType("G", typeof(A).Name, "property", typeof(B).Name),
Assert.Throws<InvalidOperationException>(() => a.AddProperty(A.GProperty)).Message);
}

Expand All @@ -426,7 +426,7 @@ public void Adding_property_throws_when_grandchild_type_has_property_with_same_n
d.AddProperty(A.GProperty);

Assert.Equal(
CoreStrings.ConflictingPropertyOrNavigation("G", typeof(A).Name, typeof(D).Name),
CoreStrings.ConflictingPropertyOrNavigationOnBaseType("G", typeof(A).Name, "property", typeof(D).Name),
Assert.Throws<InvalidOperationException>(() => a.AddProperty(A.GProperty)).Message);
}

Expand Down
Loading
Loading