dotnet/efcore · error · InvalidOperationException
' . ' and ' . ' are both mapped to column ' ' in ' ', but…
Error message
'{entityType1}.{property1}' and '{entityType2}.{property2}' are both mapped to column '{columnName}' in '{table}', but the properties are contained within the same hierarchy. All properties on an entity type must be mapped to different columns. What it means
Two properties that live in the same inheritance hierarchy were mapped to the same column on a table/view. Within a single hierarchy, EF requires every property to map to a distinct column because the framework treats the column set as the union of all derived types' properties and cannot resolve ambiguity. The IsAssignableFrom check between the two declaring types confirms they are in one hierarchy (sibling/derived).
Solutions
- Rename one of the properties' column via HasColumnName("<unique-name>") so they no longer collide.
- Consolidate the two properties into a single property declared on the common base type.
- If they genuinely represent the same value, map them to one property on the base type instead of two.
- Switch the conflicting derived types to TPT/TPC so they live in separate tables.
Example fix
// before
modelBuilder.Entity<Manager>().Property(m => m.DepartmentName).HasColumnName("Name");
modelBuilder.Entity<Engineer>().Property(e => e.ProjectName).HasColumnName("Name"); // same hierarchy
// after
modelBuilder.Entity<Manager>().Property(m => m.DepartmentName).HasColumnName("DepartmentName");
modelBuilder.Entity<Engineer>().Property(e => e.ProjectName).HasColumnName("ProjectName"); Defensive patterns
Strategy: validation
Validate before calling
// Detect properties in one hierarchy mapped to the same column.
var groups = hierarchyTypes.SelectMany(t => t.GetProperties())
.Where(p => p.GetColumnName(storeObject) is string c)
.GroupBy(p => p.GetColumnName(storeObject)!)
.Where(g => g.Count() > 1
&& g.Select(p => p.DeclaringType).Distinct().Any(d1 => g.Any(p2 => p2.DeclaringType != d1 && (d1.IsAssignableFrom(p2.DeclaringType) || p2.DeclaringType.IsAssignableFrom(d1)))));
foreach (var g in groups) Console.WriteLine($"Conflict on column {g.Key}"); Try / catch
try { _ = db.Model; } catch (InvalidOperationException ex) when (ex.Message.Contains("contained within the same hierarchy")) { // rename one of the conflicting columns } Prevention
- Never call HasColumnName with the same value on properties declared in different branches of a hierarchy.
- Use a naming convention derived from the type+property to avoid accidental collisions.
- Review column names when merging two previously-separate hierarchies.
When it happens
Trigger: An inheritance hierarchy (TPH/TPT) where two sibling derived types both have a property with the same HasColumnName, or a base and derived type each declare a property mapped to the same column name. Thrown in ValidateSharedColumnsCompatibility when property.DeclaringType.IsAssignableFrom(duplicateProperty.DeclaringType) or vice versa.
Common situations: Two derived entities (e.g. Manager and Employee) both having a 'Name' column with HasColumnName("Name"); renaming a property but not its column; merging two hierarchies that previously had separate tables into one TPH table.
Related errors
- ' . ' and ' . ' are both mapped to column ' ' in ' ', but…
- ' . ' and ' . ' are both mapped to column ' ' in ' ', but…
- ' . ' and ' . ' are both mapped to column ' ' in ' ', but…
- ' . ' and ' . ' are both mapped to column ' ' in ' ', but…
- ' . ' and ' . ' are both mapped to column ' ' in ' ', but…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/ba43f451777a9655.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:1509
foreach (var property in structuralType.GetDeclaredProperties())
{
var columnName = property.GetColumnName(storeObject);
if (columnName == null)
{
continue;
}
missingConcurrencyTokens?.Remove(columnName);
if (!propertyMappings.TryGetValue(columnName, out var duplicateProperty))
{
propertyMappings[columnName] = property;
continue;
}
if (property.DeclaringType.IsAssignableFrom(duplicateProperty.DeclaringType)
|| duplicateProperty.DeclaringType.IsAssignableFrom(property.DeclaringType))
{
throw new InvalidOperationException(
RelationalStrings.DuplicateColumnNameSameHierarchy(
duplicateProperty.DeclaringType.DisplayName(),
duplicateProperty.Name,
property.DeclaringType.DisplayName(),
property.Name,
columnName,
storeObject.DisplayName()));
}
this.ValidateCompatible(property, duplicateProperty, columnName, storeObject, logger);
}
foreach (var complexProperty in structuralType.GetDeclaredComplexProperties())
{
ValidateCompatible(complexProperty.ComplexType, storeObject, propertyMappings, missingConcurrencyTokens, logger);
}
}
}View on GitHub (pinned to 3a2006ef56)