Skip to content

Add Named Default Constraints to What's New #5035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions entity-framework/core/modeling/generated-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---

Expand All @@ -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

Expand Down
33 changes: 33 additions & 0 deletions entity-framework/core/what-is-new/ef-core-10.0/whatsnew.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).

<a name="default-constrain-names"></a>

## 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<Blog>()
.Property(b => b.IsActive)
.HasDefaultValue(true, "DF_Blog_IsActive");

modelBuilder.Entity<Post>()
.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();
}

```

<a name="other-improvements"></a>

## Other improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ internal class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

#region DefaultValue
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
#region DefaultValue
modelBuilder.Entity<Blog>()
.Property(b => b.Rating)
.HasDefaultValue(3);
#endregion

#region DefaultValueNamed
modelBuilder.Entity<Blog>()
.Property(b => b.Rating)
.HasDefaultValue(3, "DF_Blog_IsActive");
#endregion
}
#endregion
}

public class Blog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ internal class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

#region DefaultValueSql
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
#region DefaultValueSql
modelBuilder.Entity<Blog>()
.Property(b => b.Created)
.HasDefaultValueSql("getdate()");
#endregion

#region DefaultValueSqlNamed
modelBuilder.Entity<Blog>()
.Property(b => b.Created)
.HasDefaultValueSql("getdate()" , "DF_Blog_IsActive");
#endregion
}
#endregion
}

public class Blog
Expand Down
Loading