You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Runs on every context creation — wasteful since the DB already exists after the first call
Conflicts with the existing migration — 20250812090000_InitialSQLiteMigration.cs exists but never runs because EnsureCreated() takes precedence
This means any schema change (new index, column, table) only takes effect on fresh installs, not for existing users.
Concrete example
PR #467 added a composite index IX_Samples_SessionTime on (LoggingSessionID, TimestampTicks) to speed up ordered session queries. This index will never be created on existing user databases.
Proposed Solution
Replace EnsureCreated() with Database.Migrate() at app startup.
Steps
Remove Database.EnsureCreated() from the LoggingContext constructor
Add a one-time Database.Migrate() call in App.OnStartup() (before any other DB access)
Create a new migration for the composite index:
dotnet ef migrations add AddSamplesSessionTimeIndex
Handle the upgrade path for existing databases that were created by EnsureCreated() (no __EFMigrationsHistory table):
// At startup, before Migrate():usingvarcontext=factory.CreateDbContext();if(!context.Database.GetAppliedMigrations().Any()&&context.Database.GetPendingMigrations().Any()){// Existing DB created by EnsureCreated — seed migration history// so Migrate() doesn't try to recreate existing tablescontext.Database.ExecuteSqlRaw("CREATE TABLE IF NOT EXISTS __EFMigrationsHistory "+"(MigrationId TEXT NOT NULL PRIMARY KEY, ProductVersion TEXT NOT NULL)");context.Database.ExecuteSqlRaw("INSERT OR IGNORE INTO __EFMigrationsHistory "+"VALUES ('20250812090000_InitialSQLiteMigration', '9.0.0')");}context.Database.Migrate();
Update test setup to use Migrate() instead of EnsureCreated()
Risks & Mitigation
Risk
Severity
Mitigation
Existing users' DBs fail on upgrade
High
Seed __EFMigrationsHistory with initial migration before calling Migrate()
Data loss if migration fails
Medium
Back up the DB file at startup before first Migrate() call (SQLite = single file copy)
Missed code paths still calling EnsureCreated()
Low
Search entire codebase — currently only 1 call site
Key files
Daqifi.Desktop/Loggers/LoggingContext.cs — remove EnsureCreated() from constructor
Problem
LoggingContextcallsDatabase.EnsureCreated()in its constructor, which:20250812090000_InitialSQLiteMigration.csexists but never runs becauseEnsureCreated()takes precedenceThis means any schema change (new index, column, table) only takes effect on fresh installs, not for existing users.
Concrete example
PR #467 added a composite index
IX_Samples_SessionTimeon(LoggingSessionID, TimestampTicks)to speed up ordered session queries. This index will never be created on existing user databases.Proposed Solution
Replace
EnsureCreated()withDatabase.Migrate()at app startup.Steps
Database.EnsureCreated()from theLoggingContextconstructorDatabase.Migrate()call inApp.OnStartup()(before any other DB access)EnsureCreated()(no__EFMigrationsHistorytable):Migrate()instead ofEnsureCreated()Risks & Mitigation
__EFMigrationsHistorywith initial migration before callingMigrate()Migrate()call (SQLite = single file copy)EnsureCreated()Key files
Daqifi.Desktop/Loggers/LoggingContext.cs— removeEnsureCreated()from constructorDaqifi.Desktop/App.xaml.cs— add startup migration logicDaqifi.Desktop/Migrations/— existing migration folder, add new migration for indexContext