-
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
Merged
AndriySvyryd
merged 14 commits into
main
from
copilot/fix-a7ed9dfc-f2b7-437e-800c-8d27e171c91a
Jan 6, 2026
Merged
Changes from all commits
Commits
Show all changes
14 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 f269028
Address review feedback: improve docs clarity, add context, fix consi…
Copilot 0022d6e
Merge branch 'main' into copilot/fix-a7ed9dfc-f2b7-437e-800c-8d27e171…
AndriySvyryd 1ece8b6
Fix code highlight reference and remove extra blank line
Copilot 3685cdd
Update entity-framework/core/providers/sqlite/value-generation.md
AndriySvyryd 95e2b82
Update entity-framework/core/providers/sqlite/value-generation.md
AndriySvyryd a0f528b
Update entity-framework/core/providers/sqlite/value-generation.md
AndriySvyryd 6b2b3c0
Update entity-framework/core/providers/sqlite/value-generation.md
AndriySvyryd 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
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
| --- | ||
| 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](https://sqlite.org/autoinc.html). Starting with EF Core 10, SQLite AUTOINCREMENT can also be enabled or disabled via configuration. | ||
|
|
||
| ### Configuring AUTOINCREMENT | ||
|
|
||
| By convention, integer primary keys are automatically configured with AUTOINCREMENT when they are not part of a composite key 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=4)] | ||
|
|
||
| ## Disabling AUTOINCREMENT for default SQLite value generation | ||
|
|
||
| AUTOINCREMENT imposes extra CPU, memory, disk space, and disk I/O overhead compared to the default key generation algorithm in SQLite - [ROWID](https://sqlite.org/lang_createtable.html#rowid). The downside of `ROWID` is that it reuses values from deleted rows. If your scenario wouldn't be affected by this, you may want to disable AUTOINCREMENT and use SQLite's default value generation behavior instead. You can do this using the Metadata API: | ||
|
|
||
| <!-- | ||
| protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| { | ||
| modelBuilder.Entity<Blog>() | ||
| .Property(b => b.Id) | ||
| .Metadata.SetValueGenerationStrategy(SqliteValueGenerationStrategy.None); | ||
| } | ||
| --> | ||
| [!code-csharp[Main](../../../../samples/core/Sqlite/ValueGeneration/SqliteValueGenerationStrategyNone.cs?name=SqliteValueGenerationStrategyNone&highlight=5)] | ||
|
|
||
| Alternatively, you can configure EF to not treat the property as value-generated: | ||
|
|
||
| <!-- | ||
| protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| { | ||
| modelBuilder.Entity<Blog>() | ||
| .Property(b => b.Id) | ||
| .ValueGeneratedNever(); | ||
| } | ||
| --> | ||
| [!code-csharp[Main](../../../../samples/core/Sqlite/ValueGeneration/SqliteValueGeneratedNever.cs?name=SqliteValueGeneratedNever&highlight=5)] | ||
|
|
||
| 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](https://sqlite.org/lang_createtable.html#rowids_and_the_integer_primary_key), change the column type from `INTEGER` to `INT`. | ||
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<Blog> Blogs { get; set; } | ||
|
|
||
| #region SqliteAutoincrementWithValueConverter | ||
| protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| { | ||
| modelBuilder.Entity<Blog>() | ||
| .Property(b => b.Id) | ||
| .HasConversion<int>() | ||
| .UseAutoincrement(); | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| #endregion | ||
|
|
||
| protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
| => optionsBuilder.UseSqlite("Data Source=sample.db"); | ||
| } | ||
|
|
||
| public class Blog | ||
| { | ||
| public BlogId Id { get; set; } | ||
| public string Title { get; set; } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
samples/core/Sqlite/ValueGeneration/SqliteValueGeneratedNever.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,20 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
|
|
||
| namespace EFCore.Sqlite.ValueGeneration; | ||
|
|
||
| public class SqliteValueGeneratedNeverContext : DbContext | ||
| { | ||
| public DbSet<Blog> Blogs { get; set; } | ||
|
|
||
| #region SqliteValueGeneratedNever | ||
| protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| { | ||
| modelBuilder.Entity<Blog>() | ||
| .Property(b => b.Id) | ||
| .ValueGeneratedNever(); | ||
| } | ||
| #endregion | ||
|
|
||
| protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
| => optionsBuilder.UseSqlite("Data Source=sample.db"); | ||
| } |
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<Blog> Blogs { get; set; } | ||
|
|
||
| #region SqliteValueGenerationStrategyNone | ||
| protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| { | ||
| modelBuilder.Entity<Blog>() | ||
| .Property(b => b.Id) | ||
| .Metadata.SetValueGenerationStrategy(SqliteValueGenerationStrategy.None); | ||
| } | ||
| #endregion | ||
|
|
||
| protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
| => optionsBuilder.UseSqlite("Data Source=sample.db"); | ||
| } | ||
|
|
||
| public class Blog | ||
| { | ||
| public int Id { get; set; } | ||
| public string Title { get; set; } | ||
| } |
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.