using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; namespace ErsatzTV.Infrastructure.Sqlite.Data; /// /// Classifies SQLite provider exceptions wrapped by EF Core. Wired to /// at startup. /// public static class SqliteErrorClassifier { // SQLITE_CONSTRAINT (primary result code 19), narrowed to the two extended codes that mean a // duplicate key: SQLITE_CONSTRAINT_PRIMARYKEY (1555) and SQLITE_CONSTRAINT_UNIQUE (2067). Other // constraint failures (FK 787, NOT NULL 1299, CHECK 275) are deliberately NOT treated as duplicates. private const int SqliteConstraint = 19; private const int SqliteConstraintPrimaryKey = 1555; private const int SqliteConstraintUnique = 2067; public static bool IsUniqueConstraintViolation(DbUpdateException ex) => ex.InnerException is SqliteException { SqliteErrorCode: SqliteConstraint } inner && inner.SqliteExtendedErrorCode is SqliteConstraintPrimaryKey or SqliteConstraintUnique; }