-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional changes related to value generation
* Create new page on SQL Server value generation, with information on IDENTITY, seed/increment, etc. * Delete "explicit values for generated properties" page, moving relevant content to the new SQL Server page and to the fixed-up generated properties page. Closes #2999
- Loading branch information
Showing
16 changed files
with
164 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
entity-framework/core/providers/sql-server/value-generation.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
title: Microsoft SQL Server Database Provider - Value Generation - EF Core | ||
description: Value Generation Patterns Specific to the SQL Server Entity Framework Core Database Provider | ||
author: roji | ||
ms.date: 1/10/2020 | ||
uid: core/providers/sql-server/value-generation | ||
--- | ||
# SQL Server Value Generation | ||
|
||
This page details value generation configuration and patterns that are specific to the SQL Server provider. It's recommended to first read [the general page on value generation](xref:core/modeling/generated-properties). | ||
|
||
## IDENTITY columns | ||
|
||
By convention, numeric columns that are configured to have their values generated on add are set up as [SQL Server IDENTITY columns](https://docs.microsoft.com/sql/t-sql/statements/create-table-transact-sql-identity-property). | ||
|
||
### Seed and increment | ||
|
||
By default, IDENTITY columns start off at 1 (the seed), and increment by 1 each time a row is added (the increment). You can configure a different seed and increment as follows: | ||
|
||
[!code-csharp[Main](../../../../samples/core/SqlServer/ValueGeneration/IdentityOptionsContext.cs?name=IdentityOptions&highlight=5)] | ||
|
||
> [!NOTE] | ||
> The ability to configure IDENTITY seed and increment was introduced in EF Core 3.0. | ||
### Inserting explicit values into IDENTITY columns | ||
|
||
By default, SQL Server doesn't allow inserting explicit values into IDENTITY columns. To do so, you must manually enable `IDENTITY_INSERT` before calling `SaveChanges()`, as follows: | ||
|
||
[!code-csharp[Main](../../../../samples/core/SqlServer/ValueGeneration/ExplicitIdentityValues.cs?name=ExplicitIdentityValues)] | ||
|
||
> [!NOTE] | ||
> We have a [feature request](https://github.com/aspnet/EntityFramework/issues/703) on our backlog to do this automatically within the SQL Server provider. | ||
## Sequences | ||
|
||
As an alternative to IDENTITY columns, you can use standard sequences. This can be useful in various scenarios; for example, you may want to have multiple columns drawing their default values from a single sequence. | ||
|
||
SQL Server allows you to create sequences and use them as detailed in [the general page on sequences](xref:core/modeling/sequences). It's up to you to configure your properties to use sequences via `HasDefaultValueSql()`. |
87 changes: 0 additions & 87 deletions
87
entity-framework/core/saving/explicit-values-generated-properties.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
samples/core/Modeling/FluentAPI/ValueGeneratedOnAddOrUpdateWithPropertySaveBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
|
||
namespace EFModeling.FluentAPI.ValueGeneratedOnAddOrUpdateWithPropertySaveBehavior | ||
{ | ||
class MyContext : DbContext | ||
{ | ||
public DbSet<Blog> Blogs { get; set; } | ||
|
||
#region ValueGeneratedOnAddOrUpdateWithPropertySaveBehavior | ||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Blog>().Property(b => b.LastUpdated) | ||
.ValueGeneratedOnAddOrUpdate() | ||
.Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Save); | ||
} | ||
#endregion | ||
} | ||
|
||
public class Blog | ||
{ | ||
public int BlogId { get; set; } | ||
public string Url { get; set; } | ||
public DateTime LastUpdated { get; set; } | ||
} | ||
} |
15 changes: 0 additions & 15 deletions
15
samples/core/Saving/ExplicitValuesGenerateProperties/Employee.cs
This file was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
samples/core/Saving/ExplicitValuesGenerateProperties/EmployeeContext.cs
This file was deleted.
Oops, something went wrong.
96 changes: 0 additions & 96 deletions
96
samples/core/Saving/ExplicitValuesGenerateProperties/Sample.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.