dotnet/efcore · error · InvalidOperationException
The entity type ' ' has a custom constructor binding…
Error message
The entity type '{entityType}' has a custom constructor binding. Compiled model can't be generated, because custom constructor bindings are not supported. Configure the custom constructor binding in '{customize}' in a partial '{className}' class instead. What it means
Thrown by CSharpRuntimeModelCodeGenerator when generating a compiled model for an entity type whose constructor binding is explicitly configured (overrides convention strictly) or is a FactoryMethodBinding, or whose service-only constructor binding meets the same condition. Compiled models cannot serialize arbitrary custom constructor/factory bindings, so generation is refused and the message directs you to supply the binding in a Customize() partial class instead.
Solutions
- Move the custom constructor binding into a Customize() partial class as the error message instructs, instead of relying on the compiled model to emit it.
- Remove the explicit HasConstructorBinding if it is not essential so the compiled model can use convention binding.
- Use a standard constructor binding that the compiled model can serialize.
Defensive patterns
Strategy: validation
Validate before calling
// Before compiled-model generation, check for unsupported constructor bindings
foreach (var et in model.GetEntityTypes())
{
if (et.ConstructorBinding is FactoryMethodBinding
|| (et.GetConstructorBindingConfigurationSource()?.OverridesStrictly(ConfigurationSource.Convention) ?? false))
Console.WriteLine($"Warning: {et.Name} uses a custom constructor binding; move it to Customize().");
} Try / catch
try { Optimize(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("custom constructor binding"))
{ /* move binding to Customize() partial class as instructed, then regenerate */ } Prevention
- Prefer convention constructor bindings for compiled models.
- Relocate custom/factory bindings into the generated Customize() partial class.
- Pre-flight scan the model for HasConstructorBinding before running optimize.
When it happens
Trigger: Running compiled-model generation ('dotnet ef dbcontext optimize' / ModelRuntimeInitializer code gen) for a model where an entity has HasConstructorBinding(...) with an explicit configuration source, or uses a factory-method binding.
Common situations: Configuring custom constructor bindings via HasConstructorBinding; using factory-method bindings for entity instantiation; migrating a model with custom construction to the compiled-model feature.
Related errors
- The entity type ' ' has a query filter configured. Compiled…
- The property ' . ' has a value generator configured. Use '…
- No backing field was found for property
- No files were generated in directory
- A type-qualified method call requires an instance…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/243b65ccefd94382.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs:890
.AppendLine(";");
}
mainBuilder
.AppendLine("}");
}
private void Create(IEntityType entityType, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
var runtimeEntityType = entityType as IRuntimeEntityType;
if ((entityType.ConstructorBinding is not null
&& ((runtimeEntityType?.GetConstructorBindingConfigurationSource()).OverridesStrictly(ConfigurationSource.Convention)
|| entityType.ConstructorBinding is FactoryMethodBinding))
|| (runtimeEntityType?.ServiceOnlyConstructorBinding is not null
&& (runtimeEntityType.GetServiceOnlyConstructorBindingConfigurationSource()
.OverridesStrictly(ConfigurationSource.Convention)
|| runtimeEntityType.ServiceOnlyConstructorBinding is FactoryMethodBinding)))
{
throw new InvalidOperationException(
DesignStrings.CompiledModelConstructorBinding(
entityType.ShortName(), "Customize()", parameters.ClassName));
}
if (entityType.GetDeclaredQueryFilters().Count > 0)
{
throw new InvalidOperationException(DesignStrings.CompiledModelQueryFilter(entityType.ShortName()));
}
AddNamespace(entityType.ClrType, parameters.Namespaces);
var mainBuilder = parameters.MainBuilder;
mainBuilder
.Append("var ")
.Append(parameters.TargetName)
.AppendLine(" = model.AddEntityType(")
.IncrementIndent()
.Append(_code.Literal(entityType.Name)).AppendLine(",")View on GitHub (pinned to 3a2006ef56)