{"record":{"id":"5ceb51bf148cb73f","repo":"dotnet/efcore","slug":"entitytype-is-mapped-to-the-view-view-whil","errorCode":null,"errorMessage":"'{entityType}' is mapped to the view '{view}' while '{otherEntityType}' is mapped to the view '{otherView}'. Map all the entity types in the hierarchy to the same view, or remove the discriminator and map them all to different views. See https://go.microsoft.com/fwlink/?linkid=2130430 for more information.","messagePattern":"'(.+?)' is mapped to the view '(.+?)' while '(.+?)' is mapped to the view '(.+?)'\\. Map all the entity types in the hierarchy to the same view, or remove the discriminator and map them all to different views\\. See https://go\\.microsoft\\.com/fwlink/\\?linkid=2130430 for more information\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs","lineNumber":2335,"sourceCode":"                    {\n                        throw new InvalidOperationException(\n                            RelationalStrings.StoredProcedureTphDuplicate(\n                                entityType.DisplayName(), rootEntityType.DisplayName(), rootId?.DisplayName()));\n                    }\n                }\n\n                continue;\n            }\n\n            switch (storeObjectType)\n            {\n                case StoreObjectType.Table:\n                    throw new InvalidOperationException(\n                        RelationalStrings.TphTableMismatch(\n                            entityType.DisplayName(), entityId.Value.DisplayName(),\n                            rootEntityType.DisplayName(), rootId?.DisplayName()));\n                case StoreObjectType.View:\n                    throw new InvalidOperationException(\n                        RelationalStrings.TphViewMismatch(\n                            entityType.DisplayName(), entityId.Value.DisplayName(),\n                            rootEntityType.DisplayName(), rootId?.DisplayName()));\n                case StoreObjectType.Function:\n                    throw new InvalidOperationException(\n                        RelationalStrings.TphDbFunctionMismatch(\n                            entityType.DisplayName(), entityId.Value.DisplayName(),\n                            rootEntityType.DisplayName(), rootId?.DisplayName()));\n                case StoreObjectType.InsertStoredProcedure:\n                case StoreObjectType.DeleteStoredProcedure:\n                case StoreObjectType.UpdateStoredProcedure:\n                    throw new InvalidOperationException(\n                        RelationalStrings.TphStoredProcedureMismatch(\n                            entityType.DisplayName(), entityId.Value.DisplayName(),\n                            rootEntityType.DisplayName(), rootId?.DisplayName()));\n            }\n        }\n    }","sourceCodeStart":2317,"sourceCodeEnd":2353,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L2317-L2353","documentation":"ValidateTphMapping for StoreObjectType.View: identical rule to TphTableMismatch but for views. A TPH hierarchy must map all types to the same view; a derived type resolving to a different view StoreObjectIdentifier than the root is rejected.","triggerScenarios":"Root has a discriminator (TPH) and a ToView, and a derived type was given a different .ToView(\"OtherView\"), so its StoreObjectIdentifier differs from rootId.","commonSituations":"Read-only TPH model where one derived type was pointed at a different view; scaffolding views then enabling a discriminator.","solutions":["Drop the per-derived .ToView so all types share the root view.","Remove the discriminator to switch to TPT, then map each type to its own view.","Point the derived type at the SAME view name/schema as the root."],"exampleFix":"// before\nmodelBuilder.Entity<Base>().HasDiscriminator(b => b.Kind).ToView(\"v_Base\");\nmodelBuilder.Entity<Derived>().ToView(\"v_Derived\");\n\n// after\nmodelBuilder.Entity<Base>().HasDiscriminator(b => b.Kind).ToView(\"v_Base\");\n// derived omitted, inherits root view","handlingStrategy":"validation","validationCode":"bool TphAllTypesSameView(DbContext context)\n{\n    foreach (var root in context.Model.GetEntityTypes()\n        .Where(e => e.BaseType == null && e.FindDiscriminatorProperty() != null && e.GetDerivedTypes().Any()))\n    {\n        var rootView = StoreObjectIdentifier.Create(root, StoreObjectType.View);\n        if (rootView is null) continue;\n        foreach (var d in root.GetDerivedTypes())\n            if (StoreObjectIdentifier.Create(d, StoreObjectType.View) is { } dv && dv != rootView) return false;\n    }\n    return true;\n}","typeGuard":null,"tryCatchPattern":"try { _ = context.Model; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"mapped to the view\", StringComparison.Ordinal) && ex.Message.Contains(\"same view\", StringComparison.Ordinal))\n{\n    throw new InvalidOperationException(\"A TPH hierarchy must keep all types on one view. Remove the derived .ToView or drop the discriminator for TPT.\", ex);\n}","preventionTips":["In TPH, do not set a different .ToView on derived types.","Use TPT (no discriminator) when each type needs its own view.","Smoke-test the model in your test suite."],"tags":["ef-core","model-validation","tph","views","inheritance","relational"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}