{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs#L872-L908","documentation":"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.","triggerScenarios":"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.","commonSituations":"Configuring custom constructor bindings via HasConstructorBinding; using factory-method bindings for entity instantiation; migrating a model with custom construction to the compiled-model feature.","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."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before compiled-model generation, check for unsupported constructor bindings\nforeach (var et in model.GetEntityTypes())\n{\n    if (et.ConstructorBinding is FactoryMethodBinding\n        || (et.GetConstructorBindingConfigurationSource()?.OverridesStrictly(ConfigurationSource.Convention) ?? false))\n        Console.WriteLine($\"Warning: {et.Name} uses a custom constructor binding; move it to Customize().\");\n}","typeGuard":null,"tryCatchPattern":"try { Optimize(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"custom constructor binding\"))\n{ /* move binding to Customize() partial class as instructed, then regenerate */ }","preventionTips":["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."],"tags":["compiled-model","constructor-binding","entity-types","scaffolding"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}