-
Notifications
You must be signed in to change notification settings - Fork 2k
Update SQLite AUTOINCREMENT documentation for EF Core 10 #5115
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
Open
Copilot
wants to merge
7
commits into
live
Choose a base branch
from
copilot/fix-a7ed9dfc-f2b7-437e-800c-8d27e171c91a
base: live
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+165
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0811647
Initial plan
Copilot 00a9555
Add SQLite value generation documentation and samples
Copilot 055ab79
Address feedback: restructure documentation, add EF 10 what's new not…
Copilot 7c1aa86
Update to EF 10 RC version and use new UseAutoincrement() and SetValu…
Copilot eba15dc
Add SQLite value generation to TOC
Copilot c00dee1
Address feedback: remove additional resources section, update convent…
Copilot 1440b77
Address feedback: add link to docs in what's new, remove unused sampl…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
entity-framework/core/providers/sqlite/value-generation.md
This file contains hidden or 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,71 @@ | ||
--- | ||
title: SQLite Database Provider - Value Generation - EF Core | ||
description: Value Generation Patterns Specific to the SQLite Entity Framework Core Database Provider | ||
author: AndriySvyryd | ||
ms.date: 09/26/2025 | ||
uid: core/providers/sqlite/value-generation | ||
--- | ||
# SQLite Value Generation | ||
|
||
This page details value generation configuration and patterns that are specific to the SQLite provider. It's recommended to first read [the general page on value generation](xref:core/modeling/generated-properties). | ||
|
||
## AUTOINCREMENT columns | ||
|
||
By convention, numeric primary key columns that are configured to have their values generated on add are set up with SQLite's AUTOINCREMENT feature. Starting with EF Core 10, SQLite AUTOINCREMENT is a first-class feature with full support through conventions and the Fluent API. | ||
|
||
### Configuring AUTOINCREMENT | ||
|
||
By convention, integer primary keys are automatically configured with AUTOINCREMENT when they are not composite and don't have a foreign key on them. However, you may need to explicitly configure a property to use SQLite AUTOINCREMENT when the property has a value conversion from a non-integer type, or when overriding conventions: | ||
|
||
[!code-csharp[Main](../../../../samples/core/Sqlite/ValueGeneration/SqliteAutoincrementWithValueConverter.cs?name=SqliteAutoincrementWithValueConverter&highlight=6)] | ||
|
||
## Disabling AUTOINCREMENT for default SQLite value generation | ||
|
||
In some cases, you may want to disable AUTOINCREMENT and use SQLite's default value generation behavior instead. You can do this using the Metadata API: | ||
|
||
[!code-csharp[Main](../../../../samples/core/Sqlite/ValueGeneration/SqliteValueGenerationStrategyNone.cs?name=SqliteValueGenerationStrategyNone&highlight=5)] | ||
|
||
Starting with EF Core 10, you can also use the strongly-typed Metadata API: | ||
|
||
```csharp | ||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Post>() | ||
.Property(p => p.Id) | ||
.Metadata.SetValueGenerationStrategy(SqliteValueGenerationStrategy.None); | ||
} | ||
``` | ||
|
||
Alternatively, you can disable value generation entirely: | ||
|
||
```csharp | ||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Blog>() | ||
.Property(b => b.Id) | ||
.ValueGeneratedNever(); | ||
} | ||
``` | ||
|
||
This means that it's up to the application to supply a value for the property before saving to the database. Note that this still won't disable the default value generation server-side, so non-EF usages could still get a generated value. To completely disable value generation the user can change the column type from `INTEGER` to `INT`. | ||
|
||
## Migration behavior | ||
|
||
When EF Core generates migrations for SQLite AUTOINCREMENT columns, the generated migration will include the `Sqlite:Autoincrement` annotation: | ||
|
||
```csharp | ||
migrationBuilder.CreateTable( | ||
name: "Blogs", | ||
columns: table => new | ||
{ | ||
Id = table.Column<int>(type: "INTEGER", nullable: false) | ||
.Annotation("Sqlite:Autoincrement", true), | ||
Title = table.Column<string>(type: "TEXT", nullable: true) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_Blogs", x => x.Id); | ||
}); | ||
``` | ||
|
||
This ensures that the AUTOINCREMENT feature is properly applied when the migration is executed against the SQLite database. |
This file contains hidden or 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 hidden or 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
36 changes: 36 additions & 0 deletions
36
samples/core/Sqlite/ValueGeneration/SqliteAutoincrementWithValueConverter.cs
This file contains hidden or 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,36 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace EFCore.Sqlite.ValueGeneration; | ||
|
||
public readonly struct BlogId | ||
{ | ||
public BlogId(int value) => Value = value; | ||
public int Value { get; } | ||
|
||
public static implicit operator int(BlogId id) => id.Value; | ||
public static implicit operator BlogId(int value) => new(value); | ||
} | ||
|
||
public class SqliteAutoincrementWithValueConverterContext : DbContext | ||
{ | ||
public DbSet<BlogPost> Blogs { get; set; } | ||
|
||
#region SqliteAutoincrementWithValueConverter | ||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<BlogPost>() | ||
.Property(b => b.Id) | ||
.HasConversion<int>() | ||
.UseAutoincrement(); | ||
} | ||
#endregion | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
=> optionsBuilder.UseSqlite("Data Source=sample.db"); | ||
} | ||
|
||
public class BlogPost | ||
{ | ||
public BlogId Id { get; set; } | ||
public string Title { get; set; } | ||
} |
14 changes: 14 additions & 0 deletions
14
samples/core/Sqlite/ValueGeneration/SqliteValueGeneration.csproj
This file contains hidden or 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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net10.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>disable</Nullable> | ||
<OutputType>Library</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.0-rc.1.25451.107" /> | ||
</ItemGroup> | ||
|
||
</Project> |
27 changes: 27 additions & 0 deletions
27
samples/core/Sqlite/ValueGeneration/SqliteValueGenerationStrategyNone.cs
This file contains hidden or 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 Microsoft.EntityFrameworkCore.Sqlite.Metadata; | ||
|
||
namespace EFCore.Sqlite.ValueGeneration; | ||
|
||
public class SqliteValueGenerationStrategyNoneContext : DbContext | ||
{ | ||
public DbSet<Post> Posts { get; set; } | ||
|
||
#region SqliteValueGenerationStrategyNone | ||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Post>() | ||
.Property(p => p.Id) | ||
.Metadata.SetValueGenerationStrategy(SqliteValueGenerationStrategy.None); | ||
} | ||
#endregion | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
=> optionsBuilder.UseSqlite("Data Source=sample.db"); | ||
} | ||
|
||
public class Post | ||
{ | ||
public int Id { get; set; } | ||
public string Content { get; set; } | ||
} |
This file contains hidden or 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,5 @@ | ||
{ | ||
"sdk": { | ||
"version": "10.0.100-rc.1.25451.107" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.