dotnet/efcore · error · InvalidOperationException
Explicitly named default constraints cannot be used with TPC
Error message
Explicitly named default constraints cannot be used with TPC inheritance or entity splitting. Constraint name: '{explicitDefaultConstraintName}'. What it means
Thrown by SharedTableConvention.TryUniquifyDefaultConstraint (SharedTableConvention.cs:772-773) when a property is mapped to more than one table (TPC or entity splitting) and the default constraint name IS explicitly set (HasDefaultValueConstraintName was called). EF does not currently support explicitly named default constraints in multi-table scenarios because a single explicit name cannot be shared across multiple tables. This is a known limitation (issue #27970).
Source
Thrown at src/EFCore.Relational/Metadata/Conventions/SharedTableConvention.cs:772
string? schema,
Dictionary<(string, string?), (IConventionProperty, StoreObjectIdentifier)> defaultConstraints,
in StoreObjectIdentifier storeObject,
int maxLength)
{
var mappedTables = property.GetMappedStoreObjects(StoreObjectType.Table);
if (mappedTables.Count() > 1)
{
// For TPC and some entity splitting scenarios we end up with multiple tables having to define the constraint.
// Since constraint name has to be unique, we can't keep the same name for all
// Disabling this scenario until we have better way to configure the constraint name
// see issue #27970
if (property.GetDefaultConstraintNameConfigurationSource() == null)
{
throw new InvalidOperationException(
RelationalStrings.ImplicitDefaultNamesNotSupportedForTpcWhenNamesClash(constraintName));
}
throw new InvalidOperationException(
RelationalStrings.ExplicitDefaultConstraintNamesNotSupportedForTpc(constraintName));
}
if (property.Builder.CanSetAnnotation(RelationalAnnotationNames.DefaultConstraintName, null))
{
constraintName = Uniquifier.Uniquify(constraintName, defaultConstraints, n => (n, schema), maxLength);
property.Builder.HasAnnotation(RelationalAnnotationNames.DefaultConstraintName, constraintName);
return constraintName;
}
return null;
}
private void UniquifyTriggerNames(
IConventionEntityType entityType,
Dictionary<string, (IConventionTrigger, StoreObjectIdentifier)> triggers,
in StoreObjectIdentifier storeObject,
int maxLength)View on GitHub (pinned to dbf9771522)
Solutions
- Remove the explicit default constraint name and instead avoid default values on multi-table properties, or configure defaults at the database level via raw SQL migrations.
- Switch to TPH or TPT mapping strategy where single-table default constraints work normally.
- Remove HasDefaultValueConstraintName from the property and remove the default value, then add the constraint manually in a migration via migrationBuilder.Sql().
Example fix
// before: explicit default constraint name in TPC (unsupported)
modelBuilder.Entity<Base>().UseTpcMappingStrategy();
modelBuilder.Entity<Base>().Property(b => b.Status)
.HasDefaultValue(0)
.HasDefaultValueConstraintName("DF_Status"); // throws
// after: handle defaults manually via migration SQL
// Remove default from model, add in migration:
// migrationBuilder.Sql("ALTER TABLE DerivedA ADD CONSTRAINT DF_DerivedA_Status DEFAULT 0 FOR Status");
// migrationBuilder.Sql("ALTER TABLE DerivedB ADD CONSTRAINT DF_DerivedB_Status DEFAULT 0 FOR Status"); Defensive patterns
Strategy: validation
Validate before calling
// Detect explicitly named default constraints on multi-table properties.
foreach (var et in modelBuilder.Model.GetEntityTypes())
{
foreach (var prop in et.GetDeclaredProperties())
{
if (prop.GetMappedStoreObjects(StoreObjectType.Table).Count() <= 1) continue;
if (prop.GetDefaultConstraintNameConfigurationSource() != null)
Console.WriteLine($"Explicit default constraint name on {et.DisplayName()}.{prop.Name} is not supported with TPC/splitting.");
}
} Prevention
- Do not use HasDefaultValueConstraintName on properties mapped to multiple tables (TPC/splitting).
- Handle defaults for TPC columns in migration SQL with per-table unique names.
- Consider TPH/TPT when default constraints are essential.
When it happens
Trigger: Using TPC mapping or entity splitting where a property maps to multiple tables, and calling .HasDefaultValueConstraintName("MyConstraint") — the explicit name applies to all tables but constraint names must be unique per table.
Common situations: A developer tries to fix error 495 by explicitly naming the constraint, but hits this error because explicit names are also unsupported in multi-table TPC/splitting. Setting default constraint names globally then switching to TPC.
Related errors
- Named default constraints cannot be used with TPC or entity
- Using '{methodName}' on DbSet of '{entityType}' is not suppo
- The entity type '{entityType}' cannot be instantiated becaus
- The derived entity type '{entityType}' was configured with t
- The mapping strategy '{mappingStrategy}' specified on '{enti
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/7d4579b8b16800ea.
Report an issue: GitHub.