dotnet/efcore · error · InvalidOperationException
The entity type '{entityType}' is mapped to the DbFunction n
Error message
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. What it means
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.
Source
Thrown at src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:433
/// <summary>
/// Validates the function mapping for an entity type.
/// </summary>
/// <param name="entityType">The entity type to validate.</param>
/// <param name="logger">The logger to use.</param>
protected virtual void ValidateDbFunctionMapping(
IEntityType entityType,
IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
{
var mappedFunctionName = entityType.GetFunctionName();
if (mappedFunctionName == null)
{
return;
}
var mappedFunction = entityType.Model.FindDbFunction(mappedFunctionName);
if (mappedFunction == null)
{
throw new InvalidOperationException(
RelationalStrings.MappedFunctionNotFound(entityType.DisplayName(), mappedFunctionName));
}
if (entityType.BaseType != null)
{
throw new InvalidOperationException(
RelationalStrings.InvalidMappedFunctionDerivedType(
entityType.DisplayName(), mappedFunctionName, entityType.BaseType.DisplayName()));
}
if (mappedFunction.IsScalar
|| mappedFunction.ReturnType.GetGenericArguments()[0] != entityType.ClrType)
{
throw new InvalidOperationException(
RelationalStrings.InvalidMappedFunctionUnmatchedReturn(
entityType.DisplayName(),
mappedFunctionName,
mappedFunction.ReturnType.ShortDisplayName(),View on GitHub (pinned to dbf9771522)
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.
Example fix
// before
modelBuilder.Entity<Blog>().ToFunction("GetBlogs");
// but no HasDbFunction registered
// after
modelBuilder.HasDbFunction(ctx => ctx.GetBlogs());
modelBuilder.Entity<Blog>().ToFunction(nameof(GetBlogs)); Defensive patterns
Strategy: validation
Validate before calling
// After building the model, verify each ToFunction mapping has a matching DbFunction.
foreach (var et in modelBuilder.Model.GetEntityTypes())
{
var fn = et.GetFunctionName();
if (fn != null && et.Model.FindDbFunction(fn) == null)
throw new InvalidOperationException($"{et.DisplayName()} maps to unknown DbFunction '{fn}'");
} Try / catch
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 */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The entity type '{entityType}' is mapped to the 'DbFunction'
- The entity type '{entityType}' is mapped to the 'DbFunction'
- The entity type '{entityType}' is mapped to the 'DbFunction'
- The entity type '{entityType}' was configured to use some st
- The entity type '{entityType}' was configured to use '{sproc
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/a97602a71f08bd22.
Report an issue: GitHub.