{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/Internal/CheckConstraint.cs#L28-L64","documentation":"Thrown by the CheckConstraint constructor when a check constraint with the given ModelName already exists on the SAME entity type (the internal dictionary already contains that key). EF stores check constraints per entity type keyed by name, so duplicate names within one type are ambiguous and rejected at registration time.","triggerScenarios":"Two successive HasCheckConstraint(\"ck_name\", sqlA) and HasCheckConstraint(\"ck_name\", sqlB) calls on the same entity type in OnModelCreating, or data-annotation + fluent configuration that resolve to the same constraint name. Also via the internal CheckConstraint constructor directly.","commonSituations":"Copy-pasting configuration blocks that reuse a constraint name, or merging two entity configs where the same name is applied twice. Common when sharing a base configuration helper across entity types without parameterizing the name.","solutions":["Give the second check constraint a unique name.","Remove the existing constraint before re-adding: modelBuilder.Entity<T>().HasCheckConstraint(\"ck_name\", null) then re-add, if you intend to replace it.","Audit all HasCheckConstraint calls on the entity type for the colliding name and keep a single source of truth.","If the names are auto-derived collisions, set an explicit unique Name on at least one constraint."],"exampleFix":"// before\nmodelBuilder.Entity<Order>()\n    .HasCheckConstraint(\"CK_Order_Total\", \"[Total] >= 0\")\n    .HasCheckConstraint(\"CK_Order_Total\", \"[Total] > 0\"); // duplicate name\n\n// after\nmodelBuilder.Entity<Order>()\n    .HasCheckConstraint(\"CK_Order_Total\", \"[Total] >= 0\");","handlingStrategy":"validation","validationCode":"// Guard before adding a check constraint\nvar et = modelBuilder.Entity<Order>().Metadata;\nif (et.FindCheckConstraint(\"CK_Order_Total\") is not null)\n{\n    throw new InvalidOperationException(\"CK_Order_Total already declared on Order.\");\n}\nmodelBuilder.Entity<Order>().HasCheckConstraint(\"CK_Order_Total\", \"[Total] >= 0\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep each entity type's check constraints in one configuration block to spot duplicate names.","Use a constant or nameof-based naming scheme so collisions are obvious in code review.","Remove constraints explicitly (HasCheckConstraint(name, null)) before re-adding if you intend to replace."],"tags":["efcore","check-constraint","model-building","duplicate"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}