{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/Conventions/CheckConstraintConvention.cs#L95-L131","documentation":"Thrown by CheckConstraintConvention.ProcessEntityTypeBaseTypeChanged (CheckConstraintConvention.cs:113-117) when setting a base type creates a conflict: a derived entity type has a check constraint with the same name as one on the new base type, they are incompatible (AreCompatible returns false), and all three configuration sources — the base type's check constraint, the entity's base-type setting, and the derived check constraint — are all ConfigurationSource.Explicit. EF cannot silently resolve an explicit-vs-explicit conflict.","triggerScenarios":"Defining an inheritance hierarchy (setting HasBaseType) where both the base and derived entity types explicitly declare a check constraint with the same name but different SQL. E.g., base has .ToTable(t => t.HasCheckConstraint(\"CK_Range\", \"Col > 0\")) and derived has HasCheckConstraint(\"CK_Range\", \"Col > 10\").","commonSituations":"Adding inheritance to existing entity types that independently defined check constraints with the same name. Copy-pasting check constraint configuration across types that later become part of a hierarchy. Refactoring flat types into an inheritance hierarchy.","solutions":["Rename one of the conflicting check constraints so the names are unique across the hierarchy.","Make the check constraint SQL identical on both types so AreCompatible returns true.","Remove the check constraint from the derived type and keep it only on the base type.","Lower the configuration source of one constraint below Explicit (e.g., via model configuration) so the convention can auto-resolve."],"exampleFix":"// before: same name, incompatible SQL, both explicit\nmodelBuilder.Entity<Base>().ToTable(t => t.HasCheckConstraint(\"CK_Range\", \"Value > 0\"));\nmodelBuilder.Entity<Derived>().HasBaseType<Base>()\n    .ToTable(t => t.HasCheckConstraint(\"CK_Range\", \"Value > 100\")); // throws\n\n// after: unique names\nmodelBuilder.Entity<Base>().ToTable(t => t.HasCheckConstraint(\"CK_Base_Range\", \"Value > 0\"));\nmodelBuilder.Entity<Derived>().HasBaseType<Base>()\n    .ToTable(t => t.HasCheckConstraint(\"CK_Derived_Range\", \"Value > 100\"));","handlingStrategy":"validation","validationCode":"// Before setting base type, check for duplicate check constraint names.\nvar baseNames = baseEntityType.GetCheckConstraints().Select(c => c.ModelName).ToHashSet();\nforeach (var cc in derivedEntityType.GetDerivedTypesInclusive().SelectMany(e => e.GetDeclaredCheckConstraints()))\n{\n    if (baseNames.Contains(cc.ModelName))\n        Console.WriteLine($\"Check constraint '{cc.ModelName}' on {cc.EntityType.DisplayName()} conflicts with base type.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use a naming convention that prefixes check constraint names with the entity type name to avoid collisions.","When introducing inheritance, audit check constraints on all types for name collisions.","Run a model validation test after adding HasBaseType to catch conflicts early."],"tags":["check-constraint","inheritance","convention"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}