{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L429-L465","documentation":"Thrown when a mapped DbFunction's return type does not match the entity type. The function must return IQueryable<TEntityClrType> exactly; the validator rejects scalar DbFunctions or any IQueryable<T> whose element type differs from the entity's ClrType. The first generic argument of the return type must equal entityType.ClrType.","triggerScenarios":"Pointing HasDbFunction at a method returning IQueryable<OtherType>; a scalar DbFunction; a method returning a single entity instead of IQueryable<T>; ToFunction on an entity whose DbFunction returns a different type.","commonSituations":"Reusing an existing DbFunction for a different entity; refactoring the DbFunction method signature; copy-paste between entities leaving a stale return type.","solutions":["Change the DbFunction method to return IQueryable<{YourEntity}>.","Ensure the element type exactly equals the entity's ClrType (no base/derived substitution).","Register a separate parameterless scalar DbFunction if you actually want a scalar value, and remove the entity ToFunction mapping."],"exampleFix":"// before\npublic IQueryable<Post> GetBlogs() => ...;\nmodelBuilder.Entity<Blog>().ToFunction(\"GetBlogs\");\n\n// after\npublic IQueryable<Blog> GetBlogs() => FromSqlInterpolated(...);\nmodelBuilder.Entity<Blog>().ToFunction(\"GetBlogs\");","handlingStrategy":"validation","validationCode":"foreach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    var fn = et.GetFunctionName();\n    if (fn == null) continue;\n    var dbFn = et.Model.FindDbFunction(fn);\n    if (dbFn.IsScalar || dbFn.ReturnType.GetGenericArguments().FirstOrDefault() != et.ClrType)\n        throw new InvalidOperationException($\"DbFunction {fn} return type must be IQueryable<{et.ClrType.Name}>\");\n}","typeGuard":"// Guard a method returns the right IQueryable element type\nstatic bool ReturnsEntityQueryable<T>(MethodInfo m) =>\n    m.ReturnType == typeof(IQueryable<T>);","tryCatchPattern":"try { _ = ctx.Model; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"returns\")) { /* align DbFunction return type with entity ClrType */ }","preventionTips":["Declare DbFunction methods as public IQueryable<TEntity> on the DbContext.","Keep a test that checks each entity-mapped DbFunction returns IQueryable of the entity type."],"tags":["ef-core","dbfunction","mapping","return-type","model-validation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}