{"record":{"id":"3ce9883fff1f6e8f","repo":"dotnet/efcore","slug":"the-entity-type-entitytype-is-mapped-to-the-d","errorCode":null,"errorMessage":"The entity type '{entityType}' is mapped to the 'DbFunction' named '{functionName}', but is derived from '{baseEntityType}'. Derived entity types cannot be mapped to a function.","messagePattern":"The entity type '(.+?)' is mapped to the 'DbFunction' named '(.+?)', but is derived from '(.+?)'\\. Derived entity types cannot be mapped to a function\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs","lineNumber":439,"sourceCode":"        IEntityType entityType,\n        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)\n    {\n        var mappedFunctionName = entityType.GetFunctionName();\n        if (mappedFunctionName == null)\n        {\n            return;\n        }\n\n        var mappedFunction = entityType.Model.FindDbFunction(mappedFunctionName);\n        if (mappedFunction == null)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.MappedFunctionNotFound(entityType.DisplayName(), mappedFunctionName));\n        }\n\n        if (entityType.BaseType != null)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.InvalidMappedFunctionDerivedType(\n                    entityType.DisplayName(), mappedFunctionName, entityType.BaseType.DisplayName()));\n        }\n\n        if (mappedFunction.IsScalar\n            || mappedFunction.ReturnType.GetGenericArguments()[0] != entityType.ClrType)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.InvalidMappedFunctionUnmatchedReturn(\n                    entityType.DisplayName(),\n                    mappedFunctionName,\n                    mappedFunction.ReturnType.ShortDisplayName(),\n                    entityType.ClrType.ShortDisplayName()));\n        }\n\n        if (mappedFunction.Parameters.Count > 0)\n        {\n            var parameters = \"{\"","sourceCodeStart":421,"sourceCodeEnd":457,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L421-L457","documentation":"Thrown when a derived entity type (one with a non-null BaseType) is mapped to a DbFunction. A DbFunction represents the entire result set for a type, so only a base/root type may be mapped; derived types inherit the function mapping from their base. Mapping a derived type in an inheritance hierarchy is therefore rejected.","triggerScenarios":"Calling ToFunction on a derived entity in a TPH/TPT/TPC hierarchy; configuring HasDbFunction mapping on a subclass that already has a base type.","commonSituations":"Trying to give each derived type its own function-backed query source; misunderstanding that DbFunction mapping is inherited, not per-type.","solutions":["Remove the ToFunction/DbFunction mapping from the derived type and map the base/root type instead.","Restructure so the DbFunction returns the base entity type and the hierarchy maps through it."],"exampleFix":"// before\nmodelBuilder.Entity<DerivedBlog>().ToFunction(\"GetDerivedBlogs\");\n\n// after\nmodelBuilder.Entity<BaseBlog>().ToFunction(\"GetBlogs\");\n// DerivedBlog inherits the mapping","handlingStrategy":"validation","validationCode":"foreach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    if (et.GetFunctionName() != null && et.BaseType != null)\n        throw new InvalidOperationException($\"Derived type {et.DisplayName()} cannot map to a DbFunction\");\n}","typeGuard":null,"tryCatchPattern":"try { _ = ctx.Model; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Derived entity types cannot be mapped to a function\")) { /* fix the derived-type ToFunction mapping */ }","preventionTips":["Only call ToFunction on base/root types in an inheritance hierarchy.","Add a model-build test asserting derived types have no function mapping."],"tags":["ef-core","dbfunction","inheritance","mapping","model-validation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}