{"record":{"id":"7abeaf5793ba16a9","repo":"dotnet/efcore","slug":"the-view-view-cannot-be-used-for-entity-type-7abeaf","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 no relationship between their primary keys.","messagePattern":"The view '(.+?)' cannot be used for entity type '(.+?)' since it is being used for entity type '(.+?)' and there is no relationship between their primary keys\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs","lineNumber":1360,"sourceCode":"                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\n            root = mappedType;\n        }\n\n        Check.DebugAssert(root != null);\n        unvalidatedTypes.Remove(root);\n        var typesToValidate = new Queue<IEntityType>();\n        typesToValidate.Enqueue(root);\n\n        while (typesToValidate.Count > 0)\n        {\n            var entityType = typesToValidate.Dequeue();\n            var typesToValidateLeft = typesToValidate.Count;","sourceCodeStart":1342,"sourceCodeEnd":1378,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L1342-L1378","documentation":"View-mapping analog of error 393/397. Thrown by ValidateSharedViewCompatibility when two or more entity types share a view but there is no PK-to-PK FK between them and no inheritance relationship. The message lists the view, the unlinked entity type, and the root (first type to claim the view).","triggerScenarios":"builder.Entity<A>().ToView(\"V\") and builder.Entity<B>().ToView(\"V\") with no HasOne/WithOne PK FK between them and no inheritance link. ValidateSharedViewCompatibility finds root != null when processing the second type and throws.","commonSituations":"Read-only reporting views shared by multiple entity types without an explicit 1:1 relationship; refactor that renamed a view and caused a collision; using ToView for the wrong purpose (should be a query root, not a table-share).","solutions":["If the entities really represent different rows of the same view, add a 1:1 PK FK: builder.Entity<B>().HasOne<A>().WithOne().HasForeignKey<B>(b => b.Id).","Map one of them to a different view, or replace ToView with ToTable if writes are intended.","Use keyless entities (HasNoKey) and ToView for genuine query-only types so they do not collide on the PK-share path."],"exampleFix":"// before\nbuilder.Entity<Customer>().ToView(\"CustomerView\");\nbuilder.Entity<Order>().ToView(\"CustomerView\"); // unlinked\n\n// after\nbuilder.Entity<Order>(b =>\n{\n    b.ToView(\"CustomerView\");\n    b.HasOne<Customer>().WithOne().HasForeignKey<Order>(o => o.CustomerId);\n});","handlingStrategy":"validation","validationCode":"var byView = model.GetEntityTypes().Where(e => e.GetViewName() != null).GroupBy(e => e.GetViewName());\nforeach (var grp in byView)\n{\n    var list = grp.ToList();\n    for (int i = 1; i < list.Count; i++)\n    {\n        var et = list[i];\n        bool linked = et.BaseType != null && list.Any(t => t.IsAssignableFrom(et))\n            || et.FindForeignKeys(et.FindPrimaryKey()?.Properties ?? new List<Microsoft.EntityFrameworkCore.Metadata.IProperty>())\n                .Any(fk => list.Contains(fk.PrincipalEntityType) && fk.PrincipalKey.IsPrimaryKey());\n        if (!linked)\n            Console.WriteLine($\"{et.Name} shares view '{grp.Key}' with {list[0].Name} but has no link.\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use ToView for query-only keyless entities to avoid the PK-share path entirely.","If two writable types must share a view, configure a 1:1 PK FK between them.","Code-review all ToView calls to catch accidental collisions."],"tags":["ef-core","views","model-validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}