dotnet/efcore · error · InvalidOperationException
The view '{view}' cannot be used for entity type '{entityTyp
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
The view-sharing analog of error 429. When entity types share a database view, a dependent linked by a PK foreign key must not itself have a base entity type mapped to a different view — the row cannot live in two views. The validator at line 1342-1353 throws IncompatibleViewDerivedRelationship when it finds a linking FK to another shared type while the current type has a base type elsewhere.
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 dbf9771522)
Solutions
- Map the principal entity to a different view so the dependent's inheritance and view-sharing do not conflict.
- Invert the relationship so the entity currently marked dependent becomes principal.
- Unify the view mapping for the inheritance chain, or break the inheritance relationship for the shared type.
Example fix
// before: OrderViewDetail derives from BaseViewEntity mapped to 'BaseView'
// and shares view 'OrderView' with Order
modelBuilder.Entity<Order>().ToView("OrderView");
modelBuilder.Entity<OrderViewDetail>().ToView("OrderView");
modelBuilder.Entity<BaseViewEntity>().ToView("BaseView");
// after: move Order to a distinct view
modelBuilder.Entity<Order>().ToView("OrderMain");
modelBuilder.Entity<OrderViewDetail>().ToView("OrderView"); Defensive patterns
Strategy: validation
Validate before calling
foreach (var et in modelBuilder.Model.GetEntityTypes())
{
var viewName = et.GetViewName();
if (viewName == null || et.BaseType == null) continue;
var baseView = et.BaseType.GetViewName();
if (viewName == baseView) continue;
var pk = et.FindPrimaryKey();
if (pk == null) continue;
var linkingFk = et.FindForeignKeys(pk.Properties)
.FirstOrDefault(fk => fk.PrincipalKey.IsPrimaryKey()
&& fk.PrincipalEntityType.GetViewName() == viewName);
if (linkingFk != null)
throw new InvalidOperationException(
$"{et.Name} maps view '{viewName}' with a linking FK but its base maps view '{baseView}'.");
} Prevention
- Do not mix view-sharing with inheritance across different views for the same type.
- Keep query-model (view-backed) inheritance hierarchies on a single view, or break the inheritance link.
- Audit .ToView(...) calls after any inheritance refactoring.
When it happens
Trigger: Mapping an entity to a view (`.ToView("V")`) where that entity has both a base type on a different view and an identifying FK relationship to a principal on the shared view. Detected in ValidateSharedViewCompatibility.
Common situations: Using views for read models over an inheritance hierarchy where one branch was split to its own view; refactoring TPT-style view mapping; query-only entities that accidentally retain an inheritance link to a table-mapped base.
Related errors
- Both '{entityType}' and '{otherEntityType}' are mapped to th
- '{entityType}' is mapped to the view '{view}' while '{otherE
- Trigger '{trigger}' is defined on entity type '{entityType}'
- The entity type '{entityType}' is mapped to the 'DbFunction'
- Both entity type '{entityType1}' and '{entityType2}' were co
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/6aaad067246ad567.
Report an issue: GitHub.