dotnet/efcore · error · InvalidOperationException
' ' cannot be set for ' ' at the same time as ' '. Remove…
Error message
'{conflictingConfiguration}' cannot be set for '{property}' at the same time as '{existingConfiguration}'. Remove one of these configurations. What it means
A relational column may use exactly ONE server-generation strategy. This throw fires from StoreGenerationConvention.Validate when a property has both a DefaultValue and a DefaultValueSql set for the same StoreObjectIdentifier, because the provider cannot decide which one to render into the DDL/migration. EF treats these as mutually exclusive on a single column.
Solutions
- Inspect the property named in the message and find both HasDefaultValue and HasDefaultValueSql calls across all configuration sources (fluent, attributes, scaffolded files, model snapshots).
- Keep exactly one. If you want a literal CLR default, keep HasDefaultValue and delete HasDefaultValueSql; if you want a SQL expression (e.g. GETUTCDATE()), keep HasDefaultValueSql and delete HasDefaultValue.
- Regenerate the migration so the column definition no longer emits both DEFAULT and the SQL expression.
Example fix
// before
modelBuilder.Entity<Order>()
.Property(o => o.Created)
.HasDefaultValue(DateTime.UtcNow)
.HasDefaultValueSql("GETUTCDATE()");
// after
modelBuilder.Entity<Order>()
.Property(o => o.Created)
.HasDefaultValueSql("GETUTCDATE()"); Defensive patterns
Strategy: validation
Validate before calling
// Before configuring, ensure only one of the three is set for the store object
var prop = modelBuilder.Entity<Order>().Property(o => o.Created).Metadata;
var storeObj = StoreObjectIdentifier.Table("Orders");
if (prop.TryGetDefaultValue(storeObj, out _)
&& prop.GetDefaultValueSql(storeObj) is not null)
{
throw new InvalidOperationException("Cannot set both DefaultValue and DefaultValueSql on Created.");
} Prevention
- Centralize each property's value-generation config in a single IEntityTypeConfiguration so two strategies cannot both be applied.
- After scaffolding, audit any property that uses HasDefaultValue before adding HasDefaultValueSql.
- Treat 'one of DefaultValue / DefaultValueSql / ComputedColumnSql per column' as a hard rule in code review.
When it happens
Trigger: Calling both HasDefaultValue(...) and HasDefaultValueSql(...) on the same property in OnModelCreating (or in an IEntityTypeConfiguration), possibly split across partial classes or scaffolded code plus a manual override. The convention runs during model finalization when the property is mapped to a table.
Common situations: Reverse-engineering a table that has a default SQL expression and then adding HasDefaultValue in fluent config; copy-pasting column setup between entities; upgrading an older model where only DefaultValueSql existed and a developer added DefaultValue for clarity.
Related errors
- The check constraint
- The DbFunction ' ' defined on type ' ' must be either a…
- The DbFunction ' ' does not have a parameter named ' '.
- The DbFunction ' ' has an invalid return type ' '. Ensure…
- The DbFunction ' ' has an invalid return type ' '…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/951f414b2cd78089.
Report an issue: GitHub.
Appendix: 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 3a2006ef56)