dotnet/efcore · error · InvalidOperationException
The entity type '{entityType}' has a custom constructor bind
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 in CSharpRuntimeModelCodeGenerator.Create(IEntityType) (line 890, resource CompiledModelConstructorBinding) when generating a compiled model for an entity type that has a custom constructor binding (or a factory-method binding, or a service-only binding configured above convention). Compiled models cannot encode custom constructor bindings, so generation aborts and instructs you to configure the binding in a Customize() partial class instead. It is a hard limitation of the compiled-model feature.
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 dbf9771522)
Solutions
- Follow the message: keep the custom constructor binding out of the compiled model and apply it inside the generated 'Customize()' method of the partial class named in the error.
- Remove the custom constructor binding (let EF use the default parameterless/partial constructor) if compiled-model support for it is not essential.
- Re-run compiled-model generation after moving the configuration into Customize().
- Upgrade EF Core; some binding scenarios gain compiled-model support over versions.
Example fix
// before: custom constructor binding configured on the model builder
modelBuilder.Entity<Order>()
.HasConstructorBinding(
new ConstructorBinding(
typeof(Order).GetConstructor(new[] { typeof(int) })!,
new ParameterBinding(typeof(int), nameof(Order.CustomerId))));
// after: apply it in the generated Customize() partial class instead
// partial class CompiledOrderModel
// {
// partial void Customize(ModelBuilder modelBuilder)
// => modelBuilder.Entity<Order>()
// .HasConstructorBinding(...);
// } Defensive patterns
Strategy: validation
Validate before calling
// Before compiled-model generation, detect custom constructor bindings
foreach (var et in model.GetEntityTypes())
if (et.ConstructorBinding is FactoryMethodBinding
|| (et as IRuntimeEntityType)?.GetConstructorBindingConfigurationSource()
.OverridesStrictly(ConfigurationSource.Convention))
{ /* move binding into Customize() partial; do not include in compiled model */ } Try / catch
try { /* generate compiled model */ }
catch (InvalidOperationException ex) when (ex.Message.Contains("custom constructor binding"))
{ /* apply binding via generated Customize() partial, then retry */ } Prevention
- Apply custom constructor bindings via the generated Customize() partial, not the compiled model.
- Pre-check entities for FactoryMethodBinding before generating.
When it happens
Trigger: Running compiled-model generation ('dotnet ef dbcontext optimize' / CSharpRuntimeModelCodeGenerator) for a model where an entity has a ConstructorBinding that overrides ConfigurationSource.Convention, or any FactoryMethodBinding, or a service-only binding configured above convention. The guard throws CompiledModelConstructorBinding(entityType.ShortName(), 'Customize()', className).
Common situations: Configuring HasConstructorBinding / a factory method on an entity, then requesting a compiled model. Using a constructor with parameters that EF binds to properties/columns. These customizations cannot be baked into the runtime model source and must be applied at model-build time via the generated Customize() partial class.
Related errors
- Compound assignment not supported yet.
- The entity type '{entityType}' has a query filter configured
- The property '{entityType}.{property}' has a value generator
- No files were generated in directory '{outputDirectoryName}'
- You cannot add a migration with the name 'Migration'.
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/243b65ccefd94382.
Report an issue: GitHub.