public void Initialize() { SchemaInfo schemaInfo = null; if (!File.Exists(Location)) { SQLiteConnection.CreateFile(Location); schemaInfo = new SchemaInfo { Id = -1, Version = 0 }; } using (var context = new AssetsContext(Location)) { if (schemaInfo == null) { schemaInfo = context.SchemaInfos.Single(); } var migrationScript = new DatabaseMigrations(); while (schemaInfo.Version < DatabaseMigrations.RequiredDatabaseVersion) { schemaInfo.Version++; migrationScript.Migrations[schemaInfo.Version](context); context.SaveChanges(); } if (schemaInfo.Id == -1) { context.SchemaInfos.Add(schemaInfo); context.SaveChanges(); } } myIsInitialized = true; }
private void ToVersion15(AssetsContext ctx) { ctx.Database.ExecuteSqlCommand(@"ALTER TABLE Currencies ADD COLUMN Symbol TEXT;"); foreach (var currency in ctx.Currencies) { currency.Symbol = "ToBeDefined"; } ctx.SaveChanges(); UpdateTable(ctx.Database, "Currencies", @" Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Name TEXT NOT NULL, Symbol TEXT NOT NULL", "Id, Name, Symbol"); }
private void ToVersion12(AssetsContext ctx) { ctx.Database.ExecuteSqlCommand(@"ALTER TABLE Companies ADD COLUMN Guid TEXT;"); ctx.Database.ExecuteSqlCommand(@"ALTER TABLE Stocks ADD COLUMN Guid TEXT;"); foreach (var company in ctx.Companies) { company.Guid = Guid.NewGuid().ToString(); } foreach (var stock in ctx.Stocks) { stock.Guid = Guid.NewGuid().ToString(); } ctx.SaveChanges(); UpdateTable(ctx.Database, "Companies", @" Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Guid TEXT NOT NULL, Name TEXT NOT NULL, Homepage TEXT NULL, Sector TEXT NULL, Country TEXT NULL", "Id, Guid, Name, Homepage, Sector, Country"); UpdateTable(ctx.Database, "Stocks", @" Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Guid TEXT NOT NULL, Isin TEXT NOT NULL, Wpkn TEXT, Symbol TEXT, Company_id INTEGER NOT NULL, FOREIGN KEY(Company_id) REFERENCES Companies(Id)", "Id, Guid, Isin, Wpkn, Symbol, Company_Id"); }