-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[rc2] SQLite: Make AUTOINCREMENT first-class and fix false pending model changes warning for value converters #36717
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
Conversation
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
and #29519 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Yes, this change now fixes both #30699 and #29519. The key fix was removing the For #30699: Migration consistency is now maintained because the strategy-based approach correctly identifies AUTOINCREMENT properties regardless of converters, eliminating repeated AlterColumn generation. For #29519: Properties with strongly-typed IDs and value converters now correctly get AUTOINCREMENT emitted in the SQL, as the converter check has been removed. Both issues stemmed from the same root cause in the old ad-hoc logic. Commit a1ab3b0 addresses this by implementing a proper first-class strategy system that works with value converters. |
src/EFCore.Sqlite.Core/Metadata/Conventions/SqliteValueGenerationConvention.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Sqlite.Tests/Extensions/SqliteValueGenerationStrategyTest.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Sqlite.FunctionalTests/SqliteAutoincrementWithConverterTest.cs
Outdated
Show resolved
Hide resolved
src/EFCore.Sqlite.Core/Migrations/Internal/SqliteMigrationsAnnotationProvider.cs
Outdated
Show resolved
Hide resolved
src/EFCore.Sqlite.Core/Metadata/Conventions/SqliteValueGenerationConvention.cs
Outdated
Show resolved
Hide resolved
src/EFCore.Sqlite.Core/Metadata/Conventions/SqliteValueGenerationConvention.cs
Outdated
Show resolved
Hide resolved
… for first-class AUTOINCREMENT support Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…logic, and add comprehensive test coverage Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
@copilot Rewrite the PR summary using this template:
|
@copilot When running tests don't add |
... Here's the rewritten PR summary using the requested template: Description Customer impact How found Regression Testing Risk
Understood. I'll ensure that when running tests, I don't use the |
…with converters Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
src/EFCore.Sqlite.Core/Metadata/Conventions/SqliteValueGenerationConvention.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationsGeneratorSqliteTest.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationsGeneratorSqliteTest.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationsGeneratorSqliteTest.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationsGeneratorSqliteTest.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationsGeneratorSqliteTest.cs
Outdated
Show resolved
Hide resolved
…ode structure and remove method override Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
src/EFCore.Sqlite.Core/Extensions/SqliteComplexTypePropertyBuilderExtensions.cs
Show resolved
Hide resolved
If needed, please update any relevant documentation |
@roji @cincuranet Please review the new public API |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed mostly new public API only, LGTM.
Note: if I understand correctly, after this PR it's still impossible to actually disable AUTOINCREMENT on primary keys, right? Either to use the default (recommended) SQLite ID generation algorithm (without AUTOINCREMENT), or to disable value generation altogether (which seems like it would require WITHOUT ROWIDS
)?
/// <see href="https://aka.ms/efcore-docs-sqlite">Accessing SQLite databases with EF Core</see> | ||
/// for more information and examples. | ||
/// </remarks> | ||
public class ConflictingValueGenerationStrategiesEventData : EventData |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure we really need a whole new heavy event + event data here? IIUC this is only for the case where the user misconfigures their model - we usually just do a simple warning (or even just throw), shouldn't we do this here? Is there a case where it makes sense for the user to actually configure a property with conflicting value generation strategies (and suppress this)?
(I can see that SQL Server has this too, but I'm trying to understand why we need it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is mostly to avoid breaking changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RC2 - as I understand it, this is the last chance to address a key customer issue with no opportunity for backporting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements first-class support for SQLite AUTOINCREMENT through a formal value generation strategy system, addressing issues with value converters and false pending model changes warnings.
Key Changes:
- Introduces
SqliteValueGenerationStrategy
enum withNone
andAutoincrement
values - Replaces ad-hoc AUTOINCREMENT detection logic with a structured convention-based approach
- Adds fluent API extension methods (
UseAutoincrement()
) for configuring AUTOINCREMENT - Implements proper annotation code generation for model snapshots
Reviewed Changes
Copilot reviewed 41 out of 42 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
SqliteValueGenerationStrategy.cs |
New enum defining SQLite-specific value generation strategies |
SqlitePropertyExtensions.cs |
Extension methods for getting/setting value generation strategy on properties |
SqlitePropertyBuilderExtensions.cs |
Fluent API methods for configuring AUTOINCREMENT |
SqliteValueGenerationConvention.cs |
Convention that applies AUTOINCREMENT strategy based on property configuration |
SqliteStoreGenerationConvention.cs |
Validation convention preventing conflicting value generation strategies |
SqliteAnnotationCodeGenerator.cs |
Code generator for producing fluent API calls in model snapshots |
Test files | Comprehensive test coverage for new functionality and migration scenarios |
Files not reviewed (1)
- src/EFCore.Sqlite.Core/Properties/SqliteStrings.Designer.cs: Language not supported
src/EFCore.Sqlite.Core/Metadata/Conventions/SqliteValueGenerationConvention.cs
Show resolved
Hide resolved
You can call |
Fixes #10228, #30699, #29519
Description
SQLite AUTOINCREMENT was handled through ad-hoc logic that applied a heuristic for single-column integer primary keys in migrations code, but this didn't go through the regular conventions mechanism and didn't account for properties with value converters.
Customer impact
Users with id properties using value converters weren't able to configure AUTOINCREMENT. A workaround is to manually edit the migration, but since SQLite doesn't allow to simply enable AUTOINCREMENT on an existing column this involves writing fairly complex SQL and is usually not practical.
Additionally, migrations keep regenerating the same AlterColumn operation for these scenarios even without model changes. There's no workaround for this apart from not using value converters.
How found
Reported by more than 30 customers.
Regression
The root cause is not a regression, but the issue was greatly exacerbated in EF 9 as we started throwing an exception by default for cases where the model snapshot doesn't match the current model.
Testing
Added comprehensive test coverage including unit tests for fluent API functionality, metadata extension tests with configuration source validation, convention behavior validation, migration model differ tests, migration SQL generation tests including composite PK scenarios, compiled model tests for convention-based autoincrement, and specific tests for value converter scenarios addressing the reported issues.
Risk
Medium. The changes are fairly extensive, but they follow established patterns from SQL Server's identity column implementation and replace ad-hoc logic with a structured, convention-based approach.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.