diff --git a/entity-framework/core/modeling/generated-properties.md b/entity-framework/core/modeling/generated-properties.md index 5781c39f00..05babfd5dc 100644 --- a/entity-framework/core/modeling/generated-properties.md +++ b/entity-framework/core/modeling/generated-properties.md @@ -2,7 +2,7 @@ title: Generated Values - EF Core description: How to configure value generation for properties when using Entity Framework Core author: AndriySvyryd -ms.date: 1/10/2021 +ms.date: 5/27/2025 uid: core/modeling/generated-properties --- @@ -16,11 +16,28 @@ On relational databases, a column can be configured with a default value; if a r You can configure a default value on a property: -[!code-csharp[Main](../../../samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValue.cs?name=DefaultValue&highlight=5)] +[!code-csharp[Main](../../../samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValue.cs?name=DefaultValue&highlight=3)] You can also specify a SQL fragment that is used to calculate the default value: -[!code-csharp[Main](../../../samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValueSql.cs?name=DefaultValueSql&highlight=5)] +[!code-csharp[Main](../../../samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValueSql.cs?name=DefaultValueSql&highlight=3)] + +Starting with EF 10, for SQL Server you can explicitly specify the name for default value constraints, giving you more control over your database schema. + +[!code-csharp[Main](../../../samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValue.cs?name=DefaultValueNamed&highlight=3)] + +[!code-csharp[Main](../../../samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValueSql.cs?name=DefaultValueSqlNamed&highlight=3)] + +You can also call `UseNamedDefaultConstraints` to enable automatic naming of all the default constraints. Note that if you have existing migrations then the next migration you add will rename every single default constraint in your model. + +```C# +protected override void OnModelCreating(ModelBuilder modelBuilder) +{ + modelBuilder + .UseNamedDefaultConstraints(); +} + +``` ## Computed columns diff --git a/entity-framework/core/what-is-new/ef-core-10.0/whatsnew.md b/entity-framework/core/what-is-new/ef-core-10.0/whatsnew.md index d0010440be..66582ece20 100644 --- a/entity-framework/core/what-is-new/ef-core-10.0/whatsnew.md +++ b/entity-framework/core/what-is-new/ef-core-10.0/whatsnew.md @@ -185,6 +185,39 @@ await context.Blogs.ExecuteUpdateAsync(s => Thanks to [@aradalvand](https://github.com/aradalvand) for proposing and pushing for this change (in [#32018](https://github.com/dotnet/efcore/issues/32018)). + + +## Custom Default Constraint Names + +In previous versions of EF Core, when you specified a default value for a property, EF Core would always let the database automatically generate a constraint name. Now, you can explicitly specify the name for default value constraints for SQL Server, giving you more control over your database schema. + +You can now specify a constraint name when defining default values in your model configuration: + +```C# +protected override void OnModelCreating(ModelBuilder modelBuilder) +{ + modelBuilder.Entity() + .Property(b => b.IsActive) + .HasDefaultValue(true, "DF_Blog_IsActive"); + + modelBuilder.Entity() + .Property(p => b.CreatedDate) + .HasDefaultValueSql("GETDATE()", "DF_Post_CreatedDate"); +} + +``` + +You can also call `UseNamedDefaultConstraints` to enable automatic naming of all the default constraints. Note that if you have existing migrations then the next migration you add will rename every single default constraint in your model. + +```C# +protected override void OnModelCreating(ModelBuilder modelBuilder) +{ + modelBuilder + .UseNamedDefaultConstraints(); +} + +``` + ## Other improvements diff --git a/samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValue.cs b/samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValue.cs index a8f67fc1d8..18495fd83e 100644 --- a/samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValue.cs +++ b/samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValue.cs @@ -6,14 +6,20 @@ internal class MyContext : DbContext { public DbSet Blogs { get; set; } - #region DefaultValue protected override void OnModelCreating(ModelBuilder modelBuilder) { + #region DefaultValue modelBuilder.Entity() .Property(b => b.Rating) .HasDefaultValue(3); + #endregion + + #region DefaultValueNamed + modelBuilder.Entity() + .Property(b => b.Rating) + .HasDefaultValue(3, "DF_Blog_IsActive"); + #endregion } - #endregion } public class Blog diff --git a/samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValueSql.cs b/samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValueSql.cs index a344e03cc3..e222167790 100644 --- a/samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValueSql.cs +++ b/samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValueSql.cs @@ -7,14 +7,20 @@ internal class MyContext : DbContext { public DbSet Blogs { get; set; } - #region DefaultValueSql protected override void OnModelCreating(ModelBuilder modelBuilder) { + #region DefaultValueSql modelBuilder.Entity() .Property(b => b.Created) .HasDefaultValueSql("getdate()"); + #endregion + + #region DefaultValueSqlNamed + modelBuilder.Entity() + .Property(b => b.Created) + .HasDefaultValueSql("getdate()" , "DF_Blog_IsActive"); + #endregion } - #endregion } public class Blog