dotnet/efcore · error · InvalidOperationException
'{conflictingConfiguration}' cannot be set for '{property}'
Error message
'{conflictingConfiguration}' cannot be set for '{property}' at the same time as '{existingConfiguration}'. Remove one of these configurations. What it means
Thrown by StoreGenerationConvention.Validate (StoreGenerationConvention.cs:128-134) during model finalizing when a property has both a DefaultValue and a DefaultValueSql configured for the same store object. These are mutually exclusive: DefaultValue sets a literal .NET value as the column default, while DefaultValueSql sets a raw SQL expression — only one can be the column's DEFAULT. The convention enforces this at model validation time.
Source
Thrown at src/EFCore.Relational/Metadata/Conventions/StoreGenerationConvention.cs:132
Validate(declaredProperty, declaringTable);
}
}
}
/// <summary>
/// Throws if there is conflicting store generation configuration for this property.
/// </summary>
/// <param name="property">The property to check.</param>
/// <param name="storeObject">The identifier of the store object.</param>
protected virtual void Validate(
IConventionProperty property,
in StoreObjectIdentifier storeObject)
{
if (property.TryGetDefaultValue(storeObject, out _))
{
if (property.GetDefaultValueSql(storeObject) != null)
{
throw new InvalidOperationException(
RelationalStrings.ConflictingColumnServerGeneration("DefaultValue", property.Name, "DefaultValueSql"));
}
if (property.GetComputedColumnSql(storeObject) != null)
{
throw new InvalidOperationException(
RelationalStrings.ConflictingColumnServerGeneration("DefaultValue", property.Name, "ComputedColumnSql"));
}
}
else if (property.GetDefaultValueSql(storeObject) != null)
{
if (property.GetComputedColumnSql(storeObject) != null)
{
throw new InvalidOperationException(
RelationalStrings.ConflictingColumnServerGeneration("DefaultValueSql", property.Name, "ComputedColumnSql"));
}
}
}View on GitHub (pinned to dbf9771522)
Solutions
- Choose one: use HasDefaultValue(value) for a literal, or HasDefaultValueSql("SQL") for an expression — not both.
- Remove the conflicting call by passing null: .HasDefaultValue(null) before setting HasDefaultValueSql.
- Search for all configuration of the property across the model to find and remove the duplicate.
Example fix
// before: both default value and default SQL
modelBuilder.Entity<Order>()
.Property(o => o.CreatedAt)
.HasDefaultValue(DateTime.UtcNow)
.HasDefaultValueSql("GETUTCDATE()"); // throws
// after: only one
modelBuilder.Entity<Order>()
.Property(o => o.CreatedAt)
.HasDefaultValueSql("GETUTCDATE()"); Defensive patterns
Strategy: validation
Validate before calling
foreach (var et in modelBuilder.Model.GetEntityTypes())
{
foreach (var prop in et.GetDeclaredProperties())
{
foreach (var table in prop.GetMappedStoreObjects(StoreObjectType.Table))
{
if (prop.TryGetDefaultValue(table, out _) && prop.GetDefaultValueSql(table) != null)
Console.WriteLine($"Conflict: {et.DisplayName()}.{prop.Name} has both DefaultValue and DefaultValueSql on {table.Name}.");
}
}
} Prevention
- Centralize property value-generation configuration in one place per property.
- Never chain HasDefaultValue().HasDefaultValueSql() — choose one.
- Add a model validation test that scans for conflicting store-generation annotations.
When it happens
Trigger: Calling both .HasDefaultValue(someValue) and .HasDefaultValueSql("GETDATE()") on the same property. Also when one is set via data annotation / convention and the other via fluent API. The Validate method checks TryGetDefaultValue first, then if DefaultValueSql is also non-null, throws.
Common situations: Chaining configuration calls or merging config from multiple sources where both default value and default SQL are set. Migrating from HasDefaultValue to HasDefaultValueSql without removing the old call. Configuration split across partial OnModelCreating methods.
Related errors
- Default value '{value}' of type '{valueType}' cannot be set
- '{entityType1}.{property1}' and '{entityType2}.{property2}'
- The check constraint '{checkConstraint}' cannot be added to
- Named default constraints cannot be used with TPC or entity
- Explicitly named default constraints cannot be used with TPC
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/951f414b2cd78089.
Report an issue: GitHub.