dotnet/efcore · error · InvalidOperationException
The check constraint '{checkConstraint}' cannot be added to
Error message
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}'. What it means
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.
Source
Thrown at src/EFCore.Relational/Metadata/Internal/CheckConstraint.cs:46
string name,
string sql,
ConfigurationSource configurationSource)
{
EntityType = entityType;
ModelName = name;
Sql = sql;
_configurationSource = configurationSource;
var constraints = GetConstraintsDictionary(EntityType);
if (constraints == null)
{
constraints = [with(StringComparer.Ordinal)];
((IMutableEntityType)EntityType).SetOrRemoveAnnotation(RelationalAnnotationNames.CheckConstraints, constraints);
}
if (constraints.ContainsKey(name))
{
throw new InvalidOperationException(
RelationalStrings.DuplicateCheckConstraint(
name, EntityType.DisplayName(), EntityType.DisplayName()));
}
var baseCheckConstraint = entityType.BaseType?.FindCheckConstraint(name);
if (baseCheckConstraint != null)
{
throw new InvalidOperationException(
RelationalStrings.DuplicateCheckConstraint(
name, EntityType.DisplayName(), baseCheckConstraint.EntityType.DisplayName()));
}
foreach (var derivedType in entityType.GetDerivedTypes())
{
var derivedCheckConstraint = FindCheckConstraint(derivedType, name);
if (derivedCheckConstraint != null)
{
throw new InvalidOperationException(View on GitHub (pinned to dbf9771522)
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.
Example fix
// before
modelBuilder.Entity<Order>()
.HasCheckConstraint("CK_Order_Total", "[Total] >= 0")
.HasCheckConstraint("CK_Order_Total", "[Total] > 0"); // duplicate name
// after
modelBuilder.Entity<Order>()
.HasCheckConstraint("CK_Order_Total", "[Total] >= 0"); Defensive patterns
Strategy: validation
Validate before calling
// Guard before adding a check constraint
var et = modelBuilder.Entity<Order>().Metadata;
if (et.FindCheckConstraint("CK_Order_Total") is not null)
{
throw new InvalidOperationException("CK_Order_Total already declared on Order.");
}
modelBuilder.Entity<Order>().HasCheckConstraint("CK_Order_Total", "[Total] >= 0"); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The check constraints '{checkConstraint1}' on '{entityType1}
- The DbFunction '{function}' has an invalid return type '{typ
- The DbFunction '{function}' is generic. Mapping generic meth
- The DbFunction '{function}' defined on type '{type}' must be
- The DbFunction '{function}' has an invalid return type '{typ
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/91f62b921655561b.
Report an issue: GitHub.