dotnet/efcore · error · InvalidOperationException
The view ' ' cannot be used for entity type ' ' since it is…
Error message
The view '{view}' cannot be used for entity type '{entityType}' since it is being used for entity type '{otherEntityType}' and there is a relationship between their primary keys in which '{entityType}' is the dependent, but '{entityType}' has a base entity type mapped to a different view. Either map '{otherEntityType}' to a different view, or invert the relationship between '{entityType}' and '{otherEntityType}'. What it means
View-mapping analog of error 392. Thrown by ValidateSharedViewCompatibility when an entity type mapped to a shared view has a PK-to-PK FK to another sharing type AND has a base entity type mapped to a different view. Splitting an inheritance hierarchy across views while also linking PKs across view-sharers is contradictory, so the validator rejects it.
Solutions
- Map one of the types to its own view.
- Invert the relationship so the derived type is not the dependent in the PK-FK link.
- Resolve the inheritance conflict — flatten the hierarchy or remap the base type to the same view.
Example fix
// before
builder.Entity<Derived>().ToView("CustomerView");
builder.Entity<Derived>().HasOne(d => d.Principal).WithOne().HasForeignKey<Derived>(d => d.Id);
// Derived : Base, Base -> "BaseView"
// after
builder.Entity<Derived>().ToView("DerivedView"); Defensive patterns
Strategy: validation
Validate before calling
foreach (var et in model.GetEntityTypes())
{
if (et.BaseType == null) continue;
var viewName = et.GetViewName();
if (viewName == null) continue;
var pk = et.FindPrimaryKey();
if (pk == null) continue;
foreach (var fk in et.FindForeignKeys(pk.Properties))
{
if (fk.PrincipalKey.IsPrimaryKey()
&& !string.Equals(viewName, fk.PrincipalEntityType.GetViewName(), StringComparison.Ordinal))
{
Console.WriteLine($"{et.Name} shares view '{viewName}' with PK FK to {fk.PrincipalEntityType.Name} while deriving from {et.BaseType.Name}.");
}
}
} Prevention
- Do not split an inheritance hierarchy across views while also linking PKs across view-sharers.
- Use ToTable (not ToView) when writes are intended and inheritance is involved.
- Validate model in tests after every view-mapping change.
When it happens
Trigger: builder.Entity<A>().ToView("V") and builder.Entity<B>().ToView("V") where B's PK has an FK to A's PK and B derives from a base type mapped to a different view. ValidateSharedViewCompatibility detects mappedType.BaseType != null alongside the linking FK.
Common situations: Query-only view mappings combined with inheritance hierarchies; refactoring TPT to views; mixing ToView and ToTable across an inheritance chain.
Related errors
- Both ' ' and ' ' are mapped to the view ' '. All the entity…
- ' ' is mapped to the view ' ' while ' ' is mapped to the…
- An Azure Cosmos DB container name is defined on entity type
- Both ' ' and ' ' are explicitly mapped to the stored…
- Both ' ' and ' ' are mapped to the table ' '. All the…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/6aaad067246ad567.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:1348
foreach (var mappedType in mappedTypes)
{
if (mappedType.BaseType != null && unvalidatedTypes.Contains(mappedType.BaseType))
{
continue;
}
if (mappedType.FindPrimaryKey() != null
&& mappedType.FindForeignKeys(mappedType.FindPrimaryKey()!.Properties)
.Any(fk => fk.PrincipalKey.IsPrimaryKey()
&& unvalidatedTypes.Contains(fk.PrincipalEntityType)))
{
if (mappedType.BaseType != null)
{
var principalType = mappedType.FindForeignKeys(mappedType.FindPrimaryKey()!.Properties)
.First(fk => fk.PrincipalKey.IsPrimaryKey()
&& unvalidatedTypes.Contains(fk.PrincipalEntityType))
.PrincipalEntityType;
throw new InvalidOperationException(
RelationalStrings.IncompatibleViewDerivedRelationship(
storeObject.DisplayName(),
mappedType.DisplayName(),
principalType.DisplayName()));
}
continue;
}
if (root != null)
{
throw new InvalidOperationException(
RelationalStrings.IncompatibleViewNoRelationship(
storeObject.DisplayName(),
mappedType.DisplayName(),
root.DisplayName()));
}
View on GitHub (pinned to 3a2006ef56)