Skip to content

Instantly share code, notes, and snippets.

@therightstuff
Last active September 11, 2019 21:42
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 therightstuff/3e5f8bb3716da728d4fb1524ef01c913 to your computer and use it in GitHub Desktop.
Save therightstuff/3e5f8bb3716da728d4fb1524ef01c913 to your computer and use it in GitHub Desktop.
How to code Entity Framework programmable migrations
[
"201704230706451_AutomaticMigration",
"201704230705538_AutomaticMigration"
]
var configuration = new DbMigrationsConfiguration<Models.MyDBContext>();
configuration.ContextType = typeof(Models.MyDBContext);
configuration.ContextKey = "MyNamespace.Migrations.Configuration";
configuration.TargetDatabase = new DbConnectionInfo(connectionString, "System.Data.SqlClient");
configuration.AutomaticMigrationsEnabled = true;
configuration.AutomaticMigrationDataLossAllowed = true;
var migrator = new DbMigrator(configuration);
migrator.Update();
migrator.Update("201704230705538_AutomaticMigration");
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// allow db calls to work when db migrations not up to date
Database.SetInitializer<MyDBContext>(null);
base.OnModelCreating(modelBuilder);
//...
}
public sealed partial class Configuration : DbMigrationsConfiguration<MyDBContext> {
/// <summary>
/// this method allows us to programmatically seed the database, use instead of
/// protected override void Seed(Models.MyDBContext context)
/// which is for the update-database command (which we should never use)
/// </summary>
public void SeedProgrammatically(
{
Seed(context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment