{"record":{"id":"3e05c5588aec53fc","repo":"dotnet/efcore","slug":"the-entity-type-entitytype-is-mapped-to-the-d-3e05c5","errorCode":null,"errorMessage":"The entity type '{entityType}' is mapped to the 'DbFunction' named '{functionName}' with return type '{returnType}'. Ensure that the mapped function returns 'IQueryable<{clrType}>'.","messagePattern":"The entity type '(.+?)' is mapped to the 'DbFunction' named '(.+?)' with return type '(.+?)'\\. Ensure that the mapped function returns 'IQueryable<(.+?)>'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs","lineNumber":447,"sourceCode":"\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 = \"{\"\n                + string.Join(\n                    \", \",\n                    mappedFunction.Parameters.Select(p => \"'\" + p.Name + \"'\"))\n                + \"}\";\n            throw new InvalidOperationException(\n                RelationalStrings.InvalidMappedFunctionWithParameters(\n                    entityType.DisplayName(), mappedFunctionName, parameters));\n        }","sourceCodeStart":429,"sourceCodeEnd":465,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L429-L465","documentation":"Thrown in ValidateDbFunctionMapping when the function an entity is mapped to either is scalar (dbFunction.IsScalar) or its element type (ReturnType.GetGenericArguments()[0]) does not equal entityType.ClrType. See RelationalModelValidator.cs:444-453. The DbFunction must return IQueryable<TExactClrType> matching the entity.","triggerScenarios":"Calling .ToFunction on an entity while the registered DbFunction returns a scalar (int, string) or returns IQueryable<OtherType> where OtherType != the entity's CLR type. Common after refactoring a method signature or pointing ToFunction at the wrong DbFunction.","commonSituations":"Registering a scalar-valued function and then trying to map an entity to it; changing the DbFunction method's generic argument during a refactor; mixing up two similarly-named DbFunctions in the model.","solutions":["Make the DbFunction return IQueryable<TEntity> where TEntity is exactly the entity's CLR type.","Point .ToFunction at the correct IQueryable<T> DbFunction (verify by name).","If the function is meant to be scalar-only, remove the entity's .ToFunction mapping.","Re-run modelBuilder.HasDbFunction(() => TheMethod()) so the cached method signature is picked up."],"exampleFix":"// before — scalar return + entity mapping mismatch\npublic int CountOrdersForCustomer(int customerId) => throw new NotImplementedException();\nmodelBuilder.HasDbFunction(() => CountOrdersForCustomer(default));\nmodelBuilder.Entity<Order>().ToFunction(\"CountOrdersForCustomer\");\n\n// after — TVF returning the entity\npublic IQueryable<Order> OrdersForCustomer(int customerId)\n    => FromExpression(() => OrdersForCustomer(customerId));\nmodelBuilder.HasDbFunction(() => OrdersForCustomer(default));\nmodelBuilder.Entity<Order>().ToFunction(\"OrdersForCustomer\");","handlingStrategy":"validation","validationCode":"foreach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    var fnName = et.GetFunctionName();\n    if (fnName is null) continue;\n    var fn = et.Model.FindDbFunction(fnName);\n    if (fn is null) continue;\n    if (fn.IsScalar || fn.ReturnType.GetGenericArguments()[0] != et.ClrType)\n        throw new InvalidOperationException($\"DbFunction '{fnName}' must return IQueryable<{et.ClrType.Name}> to map entity '{et.Name}'.\");\n}","typeGuard":null,"tryCatchPattern":"try { _ = ctx.Model; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Ensure that the mapped function returns\"))\n{\n    logger.LogError(\"Return-type mismatch between entity and DbFunction. {Msg}\", ex.Message);\n    throw;\n}","preventionTips":["Keep the DbFunction method signature's generic argument aligned with the entity type via FromExpression(() => ...).","Unit-test the model build after signature refactors.","Don't reuse a scalar DbFunction for entity mapping."],"tags":["efcore","relational","dbfunction","entity-mapping","return-type","model-validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}