{"record":{"id":"91f62b921655561b","repo":"dotnet/efcore","slug":"the-check-constraint-checkconstraint-cannot-be-91f62b","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/Internal/CheckConstraint.cs","lineNumber":46,"sourceCode":"        string name,\n        string sql,\n        ConfigurationSource configurationSource)\n    {\n        EntityType = entityType;\n        ModelName = name;\n        Sql = sql;\n        _configurationSource = configurationSource;\n\n        var constraints = GetConstraintsDictionary(EntityType);\n        if (constraints == null)\n        {\n            constraints = [with(StringComparer.Ordinal)];\n            ((IMutableEntityType)EntityType).SetOrRemoveAnnotation(RelationalAnnotationNames.CheckConstraints, constraints);\n        }\n\n        if (constraints.ContainsKey(name))\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.DuplicateCheckConstraint(\n                    name, EntityType.DisplayName(), EntityType.DisplayName()));\n        }\n\n        var baseCheckConstraint = entityType.BaseType?.FindCheckConstraint(name);\n        if (baseCheckConstraint != null)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.DuplicateCheckConstraint(\n                    name, EntityType.DisplayName(), baseCheckConstraint.EntityType.DisplayName()));\n        }\n\n        foreach (var derivedType in entityType.GetDerivedTypes())\n        {\n            var derivedCheckConstraint = FindCheckConstraint(derivedType, name);\n            if (derivedCheckConstraint != null)\n            {\n                throw new InvalidOperationException(","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/Internal/CheckConstraint.cs#L28-L64","documentation":"A check constraint name must be unique within an entity type's hierarchy chain. This throw fires at CheckConstraint construction when the same name already exists in the constraints dictionary attached to the same entity type — i.e. you called HasCheckConstraint twice with the same name on the same entity.","triggerScenarios":"Calling modelBuilder.Entity<T>().HasCheckConstraint(\"ck_range\", \"...\") twice with the same first argument; an IEntityTypeConfiguration adding a constraint whose name duplicates one declared elsewhere on T.","commonSituations":"Two configuration classes both adding the same named check; an upgrade where a new check was added without realizing another module already registered the name; copy-paste of a check constraint block.","solutions":["Search for all HasCheckConstraint calls using the named constraint across the entity's configurations.","Either rename one of them to a distinct name, or remove the duplicate if both express the same intent.","If the two constraints genuinely differ in SQL, pick a unique name for each (the database requires unique names per table anyway)."],"exampleFix":"// before\nmodelBuilder.Entity<Product>()\n    .HasCheckConstraint(\"ck_price\", \"[Price] >= 0\")\n    .HasCheckConstraint(\"ck_price\", \"[Price] < 1000000\");\n\n// after\nmodelBuilder.Entity<Product>()\n    .HasCheckConstraint(\"ck_price_nonneg\", \"[Price] >= 0\")\n    .HasCheckConstraint(\"ck_price_cap\", \"[Price] < 1000000\");","handlingStrategy":"validation","validationCode":"// Check the name does not already exist on this entity before adding\nvar et = modelBuilder.Entity<Product>().Metadata;\nif (et.FindCheckConstraint(\"ck_price\") is not null)\n{\n    throw new InvalidOperationException(\"A check constraint named 'ck_price' already exists on Product.\");\n}\nmodelBuilder.Entity<Product>().HasCheckConstraint(\"ck_price\", \"[Price] >= 0\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Adopt a naming convention for check constraints that includes the entity name to avoid collisions.","Consolidate all check constraints for an entity into a single IEntityTypeConfiguration block.","Add a startup test that fails if any entity declares two checks with the same name."],"tags":["relational","check-constraints","model-building","metadata"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}