public static void Execute(DatabaseSchemaUpdater updater, int initialVersion, int targetVersion) { while (true) { var migrates = new Migrates(); var method = migrates.GetType().GetMethods().FirstOrDefault(m => Migrate.MigrationEnabled(m, initialVersion)); if (method != null) { method.Invoke(migrates, new object[] { updater }); initialVersion = Migrate.Get(method).TargetVersion; if (initialVersion == targetVersion) { break; } } else { break; } } if (updater.DatabaseSchemaVersion != targetVersion) { throw new NotSupportedException("Valid dynamic db version update migration failed."); } }
public DyanamicDBContext(string connectionString, int currentDDBVersion, bool forbidMigration = false) //currentDDBVersion = Config.DDBVersion : base(connectionString) { if (!DatabaseExists()) { CreateDatabase(); DatabaseSchemaUpdater versionSchemaUpdater = this.CreateDatabaseSchemaUpdater(); versionSchemaUpdater.DatabaseSchemaVersion = currentDDBVersion; versionSchemaUpdater.Execute(); } else { DatabaseSchemaUpdater versionSchemaUpdater = this.CreateDatabaseSchemaUpdater(); int version = versionSchemaUpdater.DatabaseSchemaVersion; if (version != currentDDBVersion) { if (forbidMigration) { throw new InvalidOperationException("Database migration is forbidden!"); } Migrates.Execute(versionSchemaUpdater, version, currentDDBVersion); } } try { FavoriteEntry entry = FavoriteEntries.FirstOrDefault(); } catch (Exception e) { DatabaseSchemaUpdater databaseSchemaUpdater = this.CreateDatabaseSchemaUpdater(); databaseSchemaUpdater.AddTable <FavoriteEntry>(); databaseSchemaUpdater.Execute(); } try { TileEntry entry = TileEntries.FirstOrDefault(); } catch (Exception e) { DatabaseSchemaUpdater databaseSchemaUpdater = this.CreateDatabaseSchemaUpdater(); databaseSchemaUpdater.AddTable <TileEntry>(); databaseSchemaUpdater.Execute(); } try { HistoryEntry entry = HistoryEntries.FirstOrDefault(); } catch (Exception e) { DatabaseSchemaUpdater databaseSchemaUpdater = this.CreateDatabaseSchemaUpdater(); databaseSchemaUpdater.AddTable <HistoryEntry>(); databaseSchemaUpdater.Execute(); } try { StopHistoryEntry entry = StopHistoryEntries.FirstOrDefault(); } catch (Exception e) { DatabaseSchemaUpdater databaseSchemaUpdater = this.CreateDatabaseSchemaUpdater(); databaseSchemaUpdater.AddTable <StopHistoryEntry>(); databaseSchemaUpdater.Execute(); } try { PlanningHistoryEntry entry = PlanningHistoryEntries.FirstOrDefault(); } catch (Exception e) { DatabaseSchemaUpdater databaseSchemaUpdater = this.CreateDatabaseSchemaUpdater(); databaseSchemaUpdater.AddTable <PlanningHistoryEntry>(); databaseSchemaUpdater.Execute(); } try { RecentEntry entry = RecentEntries.FirstOrDefault(); } catch (Exception e) { DatabaseSchemaUpdater databaseSchemaUpdater = this.CreateDatabaseSchemaUpdater(); databaseSchemaUpdater.AddTable <RecentEntry>(); databaseSchemaUpdater.Execute(); } }