{"record":{"id":"6aaad067246ad567","repo":"dotnet/efcore","slug":"the-view-view-cannot-be-used-for-entity-type","errorCode":null,"errorMessage":"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}'.","messagePattern":"The view '(.+?)' cannot be used for entity type '(.+?)' since it is being used for entity type '(.+?)' and there is a relationship between their primary keys in which '(.+?)' is the dependent, but '(.+?)' has a base entity type mapped to a different view\\. Either map '(.+?)' to a different view, or invert the relationship between '(.+?)' and '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs","lineNumber":1348,"sourceCode":"        foreach (var mappedType in mappedTypes)\n        {\n            if (mappedType.BaseType != null && unvalidatedTypes.Contains(mappedType.BaseType))\n            {\n                continue;\n            }\n\n            if (mappedType.FindPrimaryKey() != null\n                && mappedType.FindForeignKeys(mappedType.FindPrimaryKey()!.Properties)\n                    .Any(fk => fk.PrincipalKey.IsPrimaryKey()\n                        && unvalidatedTypes.Contains(fk.PrincipalEntityType)))\n            {\n                if (mappedType.BaseType != null)\n                {\n                    var principalType = mappedType.FindForeignKeys(mappedType.FindPrimaryKey()!.Properties)\n                        .First(fk => fk.PrincipalKey.IsPrimaryKey()\n                            && unvalidatedTypes.Contains(fk.PrincipalEntityType))\n                        .PrincipalEntityType;\n                    throw new InvalidOperationException(\n                        RelationalStrings.IncompatibleViewDerivedRelationship(\n                            storeObject.DisplayName(),\n                            mappedType.DisplayName(),\n                            principalType.DisplayName()));\n                }\n\n                continue;\n            }\n\n            if (root != null)\n            {\n                throw new InvalidOperationException(\n                    RelationalStrings.IncompatibleViewNoRelationship(\n                        storeObject.DisplayName(),\n                        mappedType.DisplayName(),\n                        root.DisplayName()));\n            }\n","sourceCodeStart":1330,"sourceCodeEnd":1366,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L1330-L1366","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before: OrderViewDetail derives from BaseViewEntity mapped to 'BaseView'\n//         and shares view 'OrderView' with Order\nmodelBuilder.Entity<Order>().ToView(\"OrderView\");\nmodelBuilder.Entity<OrderViewDetail>().ToView(\"OrderView\");\nmodelBuilder.Entity<BaseViewEntity>().ToView(\"BaseView\");\n\n// after: move Order to a distinct view\nmodelBuilder.Entity<Order>().ToView(\"OrderMain\");\nmodelBuilder.Entity<OrderViewDetail>().ToView(\"OrderView\");","handlingStrategy":"validation","validationCode":"foreach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    var viewName = et.GetViewName();\n    if (viewName == null || et.BaseType == null) continue;\n    var baseView = et.BaseType.GetViewName();\n    if (viewName == baseView) continue;\n\n    var pk = et.FindPrimaryKey();\n    if (pk == null) continue;\n    var linkingFk = et.FindForeignKeys(pk.Properties)\n        .FirstOrDefault(fk => fk.PrincipalKey.IsPrimaryKey()\n            && fk.PrincipalEntityType.GetViewName() == viewName);\n    if (linkingFk != null)\n        throw new InvalidOperationException(\n            $\"{et.Name} maps view '{viewName}' with a linking FK but its base maps view '{baseView}'.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["ef-core","views","inheritance","model-validation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}