{"record":{"id":"a97602a71f08bd22","repo":"dotnet/efcore","slug":"the-entity-type-entitytype-is-mapped-to-the-db","errorCode":null,"errorMessage":"The entity type '{entityType}' is mapped to the DbFunction named '{functionName}', but no DbFunction with that name was found in the model. Ensure that the entity type mapping is configured using the model name of a function in the model.","messagePattern":"The entity type '(.+?)' is mapped to the DbFunction named '(.+?)', but no DbFunction with that name was found in the model\\. Ensure that the entity type mapping is configured using the model name of a function in the model\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs","lineNumber":433,"sourceCode":"    /// <summary>\n    ///     Validates the function mapping for an entity type.\n    /// </summary>\n    /// <param name=\"entityType\">The entity type to validate.</param>\n    /// <param name=\"logger\">The logger to use.</param>\n    protected virtual void ValidateDbFunctionMapping(\n        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(),","sourceCodeStart":415,"sourceCodeEnd":451,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L415-L451","documentation":"Thrown during relational model validation when an entity type is configured to map to a database function but no DbFunction with that model name is registered. The validator resolves the function via entityType.Model.FindDbFunction(name); a null result means the mapping name does not match any DbFunction declared on the model (e.g. via modelBuilder.HasDbFunction). This is a pure configuration-mismatch error surfaced at model finalization.","triggerScenarios":"Calling `entityBuilder.ToFunction(\"GetBlogs\")` without a matching `modelBuilder.HasDbFunction(ctx => ctx.GetBlogs())` registration; a typo between the mapping name string and the DbFunction model name; declaring the DbFunction on a different DbContext than the one that owns the entity.","commonSituations":"Migrating a query/view to DbFunction mapping and forgetting the HasDbFunction call; renaming a DbFunction method but not the ToFunction string; splitting entities across multiple DbContexts and losing the function registration.","solutions":["Register the DbFunction on the same model with modelBuilder.HasDbFunction(e => e.YourMethod()).","Verify the name passed to ToFunction exactly matches the DbFunction's model name (case-sensitive).","Confirm the DbFunction is declared on the DbContext/model that owns the entity type, not another one."],"exampleFix":"// before\nmodelBuilder.Entity<Blog>().ToFunction(\"GetBlogs\");\n// but no HasDbFunction registered\n\n// after\nmodelBuilder.HasDbFunction(ctx => ctx.GetBlogs());\nmodelBuilder.Entity<Blog>().ToFunction(nameof(GetBlogs));","handlingStrategy":"validation","validationCode":"// After building the model, verify each ToFunction mapping has a matching DbFunction.\nforeach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    var fn = et.GetFunctionName();\n    if (fn != null && et.Model.FindDbFunction(fn) == null)\n        throw new InvalidOperationException($\"{et.DisplayName()} maps to unknown DbFunction '{fn}'\");\n}","typeGuard":null,"tryCatchPattern":"try { using var ctx = new MyContext(); ctx.Database.EnsureCreated(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"no DbFunction with that name\")) { /* surface a clear config error to the operator */ }","preventionTips":["Always pair ToFunction with a HasDbFunction registration in OnModelCreating.","Use nameof() instead of string literals for DbFunction names to keep them in sync.","Add a unit test that builds the model (ctx.Model) for every DbContext to surface validation errors at test time."],"tags":["ef-core","dbfunction","model-validation","mapping","configuration"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}