{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L1330-L1366","documentation":"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.","triggerScenarios":"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.","commonSituations":"Query-only view mappings combined with inheritance hierarchies; refactoring TPT to views; mixing ToView and ToTable across an inheritance chain.","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."],"exampleFix":"// before\nbuilder.Entity<Derived>().ToView(\"CustomerView\");\nbuilder.Entity<Derived>().HasOne(d => d.Principal).WithOne().HasForeignKey<Derived>(d => d.Id);\n// Derived : Base, Base -> \"BaseView\"\n\n// after\nbuilder.Entity<Derived>().ToView(\"DerivedView\");","handlingStrategy":"validation","validationCode":"foreach (var et in model.GetEntityTypes())\n{\n    if (et.BaseType == null) continue;\n    var viewName = et.GetViewName();\n    if (viewName == null) continue;\n    var pk = et.FindPrimaryKey();\n    if (pk == null) continue;\n    foreach (var fk in et.FindForeignKeys(pk.Properties))\n    {\n        if (fk.PrincipalKey.IsPrimaryKey()\n            && !string.Equals(viewName, fk.PrincipalEntityType.GetViewName(), StringComparison.Ordinal))\n        {\n            Console.WriteLine($\"{et.Name} shares view '{viewName}' with PK FK to {fk.PrincipalEntityType.Name} while deriving from {et.BaseType.Name}.\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["ef-core","views","inheritance","model-validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}