diff --git a/entity-framework/core/providers/sqlite/value-generation.md b/entity-framework/core/providers/sqlite/value-generation.md new file mode 100644 index 0000000000..394a078941 --- /dev/null +++ b/entity-framework/core/providers/sqlite/value-generation.md @@ -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 is implemented through conventions and the Fluent API, so it can be enabled or disabled as necessary. + +### 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=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: + + +[!code-csharp[Main](../../../../samples/core/Sqlite/ValueGeneration/SqliteValueGenerationStrategyNone.cs?name=SqliteValueGenerationStrategyNone&highlight=5)] + +Alternatively, you can disable value generation entirely: + + +[!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) the user can change the column type from `INTEGER` to `INT`. diff --git a/entity-framework/core/what-is-new/ef-core-10.0/whatsnew.md b/entity-framework/core/what-is-new/ef-core-10.0/whatsnew.md index 46499d367d..7d17b3a264 100644 --- a/entity-framework/core/what-is-new/ef-core-10.0/whatsnew.md +++ b/entity-framework/core/what-is-new/ef-core-10.0/whatsnew.md @@ -689,6 +689,7 @@ If `fieldName` is trusted or has been properly sanitized, the warning can be saf ## Other improvements +- AUTOINCREMENT can now be disabled for SQLite and is also supported for properties with value converters. For more information, see [SQLite Value Generation](xref:core/providers/sqlite/value-generation). - Stop spanning all migrations with a single transaction ([#35096](https://github.com/dotnet/efcore/issues/35096)). This reverts a change done in EF9 which caused issues in various migration scenarios. - Make SQL Server scaffolding compatible with Azure Data Explorer ([#34832](https://github.com/dotnet/efcore/pull/34832), contributed by [@barnuri](https://github.com/barnuri)). - Associate the DatabaseRoot with the scoped options instance and not the singleton options ([#34477](https://github.com/dotnet/efcore/pull/34477), contributed by [@koenigst](https://github.com/koenigst)). diff --git a/entity-framework/toc.yml b/entity-framework/toc.yml index 12b1bb3055..529e1a2800 100644 --- a/entity-framework/toc.yml +++ b/entity-framework/toc.yml @@ -423,6 +423,8 @@ href: core/providers/sqlite/limitations.md - name: Function mappings href: core/providers/sqlite/functions.md + - name: Value generation + href: core/providers/sqlite/value-generation.md - name: Spatial data displayName: GIS href: core/providers/sqlite/spatial.md diff --git a/samples/core/Sqlite/ValueGeneration/SqliteAutoincrementWithValueConverter.cs b/samples/core/Sqlite/ValueGeneration/SqliteAutoincrementWithValueConverter.cs new file mode 100644 index 0000000000..b237f7b9d2 --- /dev/null +++ b/samples/core/Sqlite/ValueGeneration/SqliteAutoincrementWithValueConverter.cs @@ -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 Blogs { get; set; } + + #region SqliteAutoincrementWithValueConverter + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .Property(b => b.Id) + .HasConversion() + .UseAutoincrement(); + } + #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; } +} \ No newline at end of file diff --git a/samples/core/Sqlite/ValueGeneration/SqliteValueGeneratedNever.cs b/samples/core/Sqlite/ValueGeneration/SqliteValueGeneratedNever.cs new file mode 100644 index 0000000000..bd886153af --- /dev/null +++ b/samples/core/Sqlite/ValueGeneration/SqliteValueGeneratedNever.cs @@ -0,0 +1,20 @@ +using Microsoft.EntityFrameworkCore; + +namespace EFCore.Sqlite.ValueGeneration; + +public class SqliteValueGeneratedNeverContext : DbContext +{ + public DbSet Blogs { get; set; } + + #region SqliteValueGeneratedNever + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .Property(b => b.Id) + .ValueGeneratedNever(); + } + #endregion + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + => optionsBuilder.UseSqlite("Data Source=sample.db"); +} diff --git a/samples/core/Sqlite/ValueGeneration/SqliteValueGeneration.csproj b/samples/core/Sqlite/ValueGeneration/SqliteValueGeneration.csproj new file mode 100644 index 0000000000..6798d6ed9c --- /dev/null +++ b/samples/core/Sqlite/ValueGeneration/SqliteValueGeneration.csproj @@ -0,0 +1,14 @@ + + + + net10.0 + enable + disable + Library + + + + + + + \ No newline at end of file diff --git a/samples/core/Sqlite/ValueGeneration/SqliteValueGenerationStrategyNone.cs b/samples/core/Sqlite/ValueGeneration/SqliteValueGenerationStrategyNone.cs new file mode 100644 index 0000000000..2fc0f5060e --- /dev/null +++ b/samples/core/Sqlite/ValueGeneration/SqliteValueGenerationStrategyNone.cs @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Sqlite.Metadata; + +namespace EFCore.Sqlite.ValueGeneration; + +public class SqliteValueGenerationStrategyNoneContext : DbContext +{ + public DbSet Blogs { get; set; } + + #region SqliteValueGenerationStrategyNone + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .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; } +} \ No newline at end of file