Skip to content

Commit f6ea56b

Browse files
committed
Add Named Default Constraints to What's New
1 parent d07978b commit f6ea56b

File tree

1 file changed

+33
-0
lines changed
  • entity-framework/core/what-is-new/ef-core-10.0

1 file changed

+33
-0
lines changed

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

0 commit comments

Comments
 (0)