{"record":{"id":"1800a61a7bdb050b","repo":"dotnet/efcore","slug":"the-check-constraint-checkconstraint-cannot-be","errorCode":null,"errorMessage":"The check constraint '{checkConstraint}' cannot be added to the entity type '{entityType}' because another check constraint with the same name already exists on entity type '{conflictingEntityType}'.","messagePattern":"The check constraint '(.+?)' cannot be added to the entity type '(.+?)' because another check constraint with the same name already exists on entity type '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/Conventions/CheckConstraintConvention.cs","lineNumber":113,"sourceCode":"    {\n        var entityType = entityTypeBuilder.Metadata;\n        if (newBaseType != null)\n        {\n            var configurationSource = entityType.GetBaseTypeConfigurationSource();\n            var baseCheckConstraints = newBaseType.GetCheckConstraints().ToDictionary(c => c.ModelName);\n            List<IConventionCheckConstraint>? checkConstraintsToBeDetached = null;\n            List<IConventionCheckConstraint>? checkConstraintsToBeRemoved = null;\n            foreach (var checkConstraint in entityType.GetDerivedTypesInclusive().SelectMany(et => et.GetDeclaredCheckConstraints()))\n            {\n                if (baseCheckConstraints.TryGetValue(checkConstraint.ModelName, out var baseCheckConstraint)\n                    && baseCheckConstraint.GetConfigurationSource().Overrides(checkConstraint.GetConfigurationSource())\n                    && !AreCompatible(checkConstraint, baseCheckConstraint))\n                {\n                    if (baseCheckConstraint.GetConfigurationSource() == ConfigurationSource.Explicit\n                        && configurationSource == ConfigurationSource.Explicit\n                        && checkConstraint.GetConfigurationSource() == ConfigurationSource.Explicit)\n                    {\n                        throw new InvalidOperationException(\n                            RelationalStrings.DuplicateCheckConstraint(\n                                checkConstraint.ModelName,\n                                checkConstraint.EntityType.DisplayName(),\n                                baseCheckConstraint.EntityType.DisplayName()));\n                    }\n\n                    checkConstraintsToBeRemoved ??= [];\n\n                    checkConstraintsToBeRemoved.Add(checkConstraint);\n                    continue;\n                }\n\n                if (baseCheckConstraint != null)\n                {\n                    checkConstraintsToBeDetached ??= [];\n\n                    checkConstraintsToBeDetached.Add(checkConstraint);\n                }","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/Conventions/CheckConstraintConvention.cs#L95-L131","documentation":"CheckConstraintConvention.ProcessEntityTypeBaseTypeChanged runs when an entity is assigned a base type. It reconciles check constraints declared on the entity/derived types with those inherited from the base. If a derived type declares a check constraint whose ModelName matches a base one, the base has Explicit configuration source, the base-type change is Explicit, the derived is Explicit, AND they are incompatible, the convention cannot silently resolve it and throws DuplicateCheckConstraint.","triggerScenarios":"Setting a base type explicitly (.HasBaseType<Base>()) on an entity that already declares a check constraint with the same name (ModelName) as one on the base, where both were configured via the Fluent API (Explicit) and their SQL/sql differs (AreCompatible returns false).","commonSituations":"Refactoring an entity into a hierarchy; copy-pasting check-constraint config between base and derived; renaming constraints such that ModelNames collide; introducing inheritance after the fact.","solutions":["Give the derived check constraint a distinct name so it does not collide with the base one.","Make the derived constraint's SQL compatible with (or identical to) the base so AreCompatible passes.","Remove the redundant derived declaration and let it inherit from the base.","Lower one of the configuration sources below Explicit (data annotation / convention) so the convention can silently remove instead of throwing."],"exampleFix":"// before — same-named, incompatible check constraints on base and derived, both explicit -> [457]\nmodelBuilder.Entity<Person>().ToTable(\"People\")\n    .HasCheckConstraint(\"CK_Age\", \"Age > 0\");\nmodelBuilder.Entity<Employee>().HasBaseType<Person>()\n    .HasCheckConstraint(\"CK_Age\", \"Age >= 18\");\n\n// after — unique name on the derived constraint (or align the SQL)\nmodelBuilder.Entity<Person>().ToTable(\"People\")\n    .HasCheckConstraint(\"CK_Age_Positive\", \"Age > 0\");\nmodelBuilder.Entity<Employee>().HasBaseType<Person>()\n    .HasCheckConstraint(\"CK_Age_Adult\", \"Age >= 18\");","handlingStrategy":"validation","validationCode":"// Before assigning a base type, detect same-named check constraints that are incompatible.\nstatic IEnumerable<string> ConflictingCheckConstraintNames(IEntityType derived, IEntityType baseType)\n{\n    var baseCks = baseType.GetCheckConstraints().ToDictionary(c => c.ModelName);\n    foreach (var c in derived.GetDerivedTypesInclusive().SelectMany(et => et.GetDeclaredCheckConstraints()))\n        if (baseCks.TryGetValue(c.ModelName, out var b) && !CheckConstraintsCompatible(c, b))\n            yield return c.ModelName;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use distinct check-constraint names across base and derived types.","Align derived constraint SQL with the base when names must match.","Run a model-build test after introducing inheritance to surface constraint collisions."],"tags":["check-constraints","inheritance","conventions","model-validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}