Beispiel #1
0
        public static bool CheckMigrationVersionAndUpgradeIfNeeded()
        {
            try
            {
                var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                using var db = new Creatures3DbContext(connectionString);
                if (!db.Database.Exists())
                {
                    return(true);                       // Let EF Create it
                }
                var compatible = false;

                try
                {
                    compatible = db.Database.CompatibleWithModel(true);
                }
                catch (Exception)
                {
                    compatible = false;
                }
                if (compatible)
                {
                    return(true);
                }

                return(RunMigrations(db));
            }
            catch (Exception ex)
            {
                var s = string.Format("Problem in MigrateIfNeeded /n/p" + ex);
                MessageBox.Show(s);
                return(false);
            }
        }
Beispiel #2
0
        private static bool RunMigrations(Creatures3DbContext db)
        {
            var upgradeDescription = MigrationHelper.MigrationDescription(db);
            var pwd = AskForUpgradePassword(upgradeDescription);

            if (pwd != "cat")
            {
                return(false);
            }
            try
            {
                //http://stackoverflow.com/questions/34699283/ef6-1-3-expects-createdon-field-in-migrationhistory-table-when-database-setiniti
                //MessageBox.Show(
                //    "You might need to the following SQL First /n/p " +
                //    "ALTER TABLE dbo.__MigrationHistory ADD CreatedOn DateTime Default GETDATE() /n/p" + "GO /n/p" +
                //    "UPDATE dbo.__MigrationHistory SET CreatedOn = GETDATE()");
                MigrationHelper.RunMigrations(db);
                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return(true);
            }
        }