Skip to content

Instantly share code, notes, and snippets.

@hadoan
Created June 8, 2018 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadoan/747ebbeb5096bb9ebf2761578b430370 to your computer and use it in GitHub Desktop.
Save hadoan/747ebbeb5096bb9ebf2761578b430370 to your computer and use it in GitHub Desktop.
Backup database & migrate
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Backup database....");
Backup();
Console.WriteLine("Migrate database ...");
var config = new Configuration();
//... (set up the config object)
DbMigrator migrator = new DbMigrator(config);
//MigratorScriptingDecorator scripter = new MigratorScriptingDecorator(migrator);
//string script = scripter.ScriptUpdate(null, null);
//Console.WriteLine("Migrate: " + script);
migrator.Update();
Console.WriteLine("Done, press Enter to exit");
Console.ReadLine();
}
private static void Backup()
{
string dbname = "MyDbName";
string sqlCommand =
@"BACKUP DATABASE [{0}] TO DISK = N'{1}' WITH NOFORMAT, NOINIT, NAME = N'MyAir-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10";
using (var conn =
new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString))
{
conn.Open();
var command = new SqlCommand(string.Format(sqlCommand, dbname,
@"C:\backup_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".bak"));
command.Connection = conn;
command.ExecuteNonQuery();
}
}
}
public sealed class Configuration : DbMigrationsConfiguration<MyContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(MyContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment