{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L421-L457","documentation":"Thrown in ValidateDbFunctionMapping when an entity type that has a function mapping also has a BaseType — i.e., it is a derived type in an inheritance hierarchy. The check at RelationalModelValidator.cs:437-442 fires after the function is found: only root entity types can be mapped to a DbFunction/TVF. Derived types inherit the root's mapping.","triggerScenarios":"Configuring .ToFunction(name) on a derived entity type configuration (modelBuilder.Entity<Derived>().ToFunction(...)) while the base type is also configured. Or owning an inheritance hierarchy where each derived type tries to map to its own TVF.","commonSituations":"Misunderstanding that TVF mapping must live on the hierarchy root; refactoring a single-type model into a hierarchy and forgetting to move the ToFunction up to the base; using per-type TPT configuration and accidentally applying ToFunction to derived configs.","solutions":["Move the .ToFunction(name) call from the derived entity to the root entity configuration.","Ensure the DbFunction returns IQueryable<RootType> and map only the root with ToFunction.","If you genuinely need per-derived-type rowsets, model separate keyless DTO entities outside the hierarchy."],"exampleFix":"// before\nmodelBuilder.Entity<Animal>().ToTable(\"Animals\");\nmodelBuilder.Entity<Dog>().ToFunction(\"dbo.Dogs\"); // Dog derives from Animal\n\n// after\npublic IQueryable<Animal> AnimalsTvf()\n    => FromExpression(() => AnimalsTvf());\nmodelBuilder.HasDbFunction(() => AnimalsTvf());\nmodelBuilder.Entity<Animal>().ToFunction(\"AnimalsTvf\"); // root only","handlingStrategy":"validation","validationCode":"foreach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    if (et.GetFunctionName() is null) continue;\n    if (et.BaseType is not null)\n        throw new InvalidOperationException($\"Derived entity '{et.Name}' cannot be mapped to a function; map the root '{et.BaseType.Name}' instead.\");\n}","typeGuard":null,"tryCatchPattern":"try { _ = ctx.Model; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Derived entity types cannot be mapped to a function\"))\n{\n    logger.LogError(\"Move .ToFunction to the hierarchy root. {Msg}\", ex.Message);\n    throw;\n}","preventionTips":["Only call .ToFunction on the root entity of a hierarchy.","In IEntityTypeConfiguration<T>, check T is the root before applying ToFunction.","Review TPT/Tpc migrations to ensure derived configs don't carry ToFunction."],"tags":["efcore","relational","dbfunction","inheritance","entity-mapping","model-validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}