{"record":{"id":"243b65ccefd94382","repo":"dotnet/efcore","slug":"the-entity-type-entitytype-has-a-custom-constr","errorCode":null,"errorMessage":"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.","messagePattern":"The entity type '(.+?)' has a custom constructor binding\\. Compiled model can't be generated, because custom constructor bindings are not supported\\. Configure the custom constructor binding in '(.+?)' in a partial '(.+?)' class instead\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs","lineNumber":890,"sourceCode":"                .AppendLine(\";\");\n        }\n\n        mainBuilder\n            .AppendLine(\"}\");\n    }\n\n    private void Create(IEntityType entityType, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)\n    {\n        var runtimeEntityType = entityType as IRuntimeEntityType;\n        if ((entityType.ConstructorBinding is not null\n                && ((runtimeEntityType?.GetConstructorBindingConfigurationSource()).OverridesStrictly(ConfigurationSource.Convention)\n                    || entityType.ConstructorBinding is FactoryMethodBinding))\n            || (runtimeEntityType?.ServiceOnlyConstructorBinding is not null\n                && (runtimeEntityType.GetServiceOnlyConstructorBindingConfigurationSource()\n                        .OverridesStrictly(ConfigurationSource.Convention)\n                    || runtimeEntityType.ServiceOnlyConstructorBinding is FactoryMethodBinding)))\n        {\n            throw new InvalidOperationException(\n                DesignStrings.CompiledModelConstructorBinding(\n                    entityType.ShortName(), \"Customize()\", parameters.ClassName));\n        }\n\n        if (entityType.GetDeclaredQueryFilters().Count > 0)\n        {\n            throw new InvalidOperationException(DesignStrings.CompiledModelQueryFilter(entityType.ShortName()));\n        }\n\n        AddNamespace(entityType.ClrType, parameters.Namespaces);\n\n        var mainBuilder = parameters.MainBuilder;\n        mainBuilder\n            .Append(\"var \")\n            .Append(parameters.TargetName)\n            .AppendLine(\" = model.AddEntityType(\")\n            .IncrementIndent()\n            .Append(_code.Literal(entityType.Name)).AppendLine(\",\")","sourceCodeStart":872,"sourceCodeEnd":908,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs#L872-L908","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before: custom constructor binding configured on the model builder\nmodelBuilder.Entity<Order>()\n    .HasConstructorBinding(\n        new ConstructorBinding(\n            typeof(Order).GetConstructor(new[] { typeof(int) })!,\n            new ParameterBinding(typeof(int), nameof(Order.CustomerId))));\n\n// after: apply it in the generated Customize() partial class instead\n// partial class CompiledOrderModel\n// {\n//     partial void Customize(ModelBuilder modelBuilder)\n//         => modelBuilder.Entity<Order>()\n//             .HasConstructorBinding(...);\n// }","handlingStrategy":"validation","validationCode":"// Before compiled-model generation, detect custom constructor bindings\nforeach (var et in model.GetEntityTypes())\n    if (et.ConstructorBinding is FactoryMethodBinding\n        || (et as IRuntimeEntityType)?.GetConstructorBindingConfigurationSource()\n            .OverridesStrictly(ConfigurationSource.Convention))\n    { /* move binding into Customize() partial; do not include in compiled model */ }","typeGuard":null,"tryCatchPattern":"try { /* generate compiled model */ }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"custom constructor binding\"))\n{ /* apply binding via generated Customize() partial, then retry */ }","preventionTips":["Apply custom constructor bindings via the generated Customize() partial, not the compiled model.","Pre-check entities for FactoryMethodBinding before generating."],"tags":["ef-core","compiled-model","constructor-binding","design-time"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}