Skip to content

Commit ac7be7f

Browse files
authored
Add Named Default Constraints docs (#5035)
1 parent d07978b commit ac7be7f

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

entity-framework/core/modeling/generated-properties.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Generated Values - EF Core
33
description: How to configure value generation for properties when using Entity Framework Core
44
author: AndriySvyryd
5-
ms.date: 1/10/2021
5+
ms.date: 5/27/2025
66
uid: core/modeling/generated-properties
77
---
88

@@ -16,11 +16,28 @@ On relational databases, a column can be configured with a default value; if a r
1616

1717
You can configure a default value on a property:
1818

19-
[!code-csharp[Main](../../../samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValue.cs?name=DefaultValue&highlight=5)]
19+
[!code-csharp[Main](../../../samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValue.cs?name=DefaultValue&highlight=3)]
2020

2121
You can also specify a SQL fragment that is used to calculate the default value:
2222

23-
[!code-csharp[Main](../../../samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValueSql.cs?name=DefaultValueSql&highlight=5)]
23+
[!code-csharp[Main](../../../samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValueSql.cs?name=DefaultValueSql&highlight=3)]
24+
25+
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.
26+
27+
[!code-csharp[Main](../../../samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValue.cs?name=DefaultValueNamed&highlight=3)]
28+
29+
[!code-csharp[Main](../../../samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValueSql.cs?name=DefaultValueSqlNamed&highlight=3)]
30+
31+
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.
32+
33+
```C#
34+
protected override void OnModelCreating(ModelBuilder modelBuilder)
35+
{
36+
modelBuilder
37+
.UseNamedDefaultConstraints();
38+
}
39+
40+
```
2441

2542
## Computed columns
2643

entity-framework/core/what-is-new/ef-core-10.0/whatsnew.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,39 @@ await context.Blogs.ExecuteUpdateAsync(s =>
185185

186186
Thanks to [@aradalvand](https://github.com/aradalvand) for proposing and pushing for this change (in [#32018](https://github.com/dotnet/efcore/issues/32018)).
187187

188+
<a name="default-constrain-names"></a>
189+
190+
## Custom Default Constraint Names
191+
192+
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.
193+
194+
You can now specify a constraint name when defining default values in your model configuration:
195+
196+
```C#
197+
protected override void OnModelCreating(ModelBuilder modelBuilder)
198+
{
199+
modelBuilder.Entity<Blog>()
200+
.Property(b => b.IsActive)
201+
.HasDefaultValue(true, "DF_Blog_IsActive");
202+
203+
modelBuilder.Entity<Post>()
204+
.Property(p => b.CreatedDate)
205+
.HasDefaultValueSql("GETDATE()", "DF_Post_CreatedDate");
206+
}
207+
208+
```
209+
210+
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.
211+
212+
```C#
213+
protected override void OnModelCreating(ModelBuilder modelBuilder)
214+
{
215+
modelBuilder
216+
.UseNamedDefaultConstraints();
217+
}
218+
219+
```
220+
188221
<a name="other-improvements"></a>
189222

190223
## Other improvements

samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValue.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ internal class MyContext : DbContext
66
{
77
public DbSet<Blog> Blogs { get; set; }
88

9-
#region DefaultValue
109
protected override void OnModelCreating(ModelBuilder modelBuilder)
1110
{
11+
#region DefaultValue
1212
modelBuilder.Entity<Blog>()
1313
.Property(b => b.Rating)
1414
.HasDefaultValue(3);
15+
#endregion
16+
17+
#region DefaultValueNamed
18+
modelBuilder.Entity<Blog>()
19+
.Property(b => b.Rating)
20+
.HasDefaultValue(3, "DF_Blog_IsActive");
21+
#endregion
1522
}
16-
#endregion
1723
}
1824

1925
public class Blog

samples/core/Modeling/GeneratedProperties/FluentAPI/DefaultValueSql.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@ internal class MyContext : DbContext
77
{
88
public DbSet<Blog> Blogs { get; set; }
99

10-
#region DefaultValueSql
1110
protected override void OnModelCreating(ModelBuilder modelBuilder)
1211
{
12+
#region DefaultValueSql
1313
modelBuilder.Entity<Blog>()
1414
.Property(b => b.Created)
1515
.HasDefaultValueSql("getdate()");
16+
#endregion
17+
18+
#region DefaultValueSqlNamed
19+
modelBuilder.Entity<Blog>()
20+
.Property(b => b.Created)
21+
.HasDefaultValueSql("getdate()" , "DF_Blog_IsActive");
22+
#endregion
1623
}
17-
#endregion
1824
}
1925

2026
public class Blog

0 commit comments

Comments
 (0)