{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L415-L451","documentation":"Thrown in ValidateDbFunctionMapping when an entity type has a function mapping (entityType.GetFunctionName() returns a value) but the model contains no DbFunction with that model-name. FindDbFunction(mappedFunctionName) at RelationalModelValidator.cs:430-435 returns null. The mapping was set (e.g., via .ToFunction(name)) without a matching HasDbFunction registration.","triggerScenarios":"Calling modelBuilder.Entity<T>().ToFunction(\"dbo.MyTvf\") without a corresponding modelBuilder.HasDbFunction(() => MyMethod(...)). Or a typo between the name passed to ToFunction and the DbFunction's configured Name. Also when a DbFunction is conditionally registered (e.g., in a provider-specific branch that didn't run).","commonSituations":"Renaming a TVF in ToFunction but forgetting to rename the HasDbFunction; copy-paste errors; refactoring IModelConfiguration that dropped the HasDbFunction call; multi-tenant/shared model where the DbFunction registration is data-driven and was skipped.","solutions":["Register the DbFunction with the exact same model-name used in ToFunction: modelBuilder.HasDbFunction(() => Foo()).HasName(\"dbo.MyTvf\").","Verify the spelling and schema qualifier (dbo. prefix) match exactly between ToFunction and HasDbFunction/HasName.","Ensure the HasDbFunction call is on the same model being finalized — check conditional registration branches and IModelCacheKeyFactory.","If you no longer intend the TVF mapping, remove the .ToFunction(name) call."],"exampleFix":"// before\nmodelBuilder.Entity<Customer>()\n    .ToFunction(\"dbo.ActiveCustomers\");\n// no matching HasDbFunction registered\n\n// after\npublic IQueryable<Customer> ActiveCustomers()\n    => FromExpression(() => ActiveCustomers());\n\nmodelBuilder.HasDbFunction(() => ActiveCustomers());\nmodelBuilder.Entity<Customer>().ToFunction(\"ActiveCustomers\");","handlingStrategy":"validation","validationCode":"// Cross-check every entity's ToFunction name resolves to a registered DbFunction.\nforeach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    var fnName = et.GetFunctionName();\n    if (fnName is null) continue;\n    if (et.Model.FindDbFunction(fnName) is null)\n        throw new InvalidOperationException($\"Entity '{et.Name}' is mapped to function '{fnName}' which is not registered via HasDbFunction.\");\n}","typeGuard":null,"tryCatchPattern":"try { _ = ctx.Model; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"no DbFunction with that name was found\"))\n{\n    logger.LogError(\"ToFunction name has no matching HasDbFunction. {Msg}\", ex.Message);\n    throw;\n}","preventionTips":["Always pair entity.ToFunction(name) with modelBuilder.HasDbFunction(() => Method()) in the same OnModelCreating block.","Use nameof or a constant for the function name on both sides to avoid typos.","Put TVF method + HasDbFunction + ToFunction in one IEntityTypeConfiguration for locality."],"tags":["efcore","relational","dbfunction","entity-mapping","tvf","model-validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}