see https://www.industrialcuriosity.com/2017/04/how-to-code-entity-framework.html for context
Last active
September 11, 2019 21:42
-
-
Save therightstuff/3e5f8bb3716da728d4fb1524ef01c913 to your computer and use it in GitHub Desktop.
How to code Entity Framework programmable migrations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
"201704230706451_AutomaticMigration", | |
"201704230705538_AutomaticMigration" | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
migrator.Update("201704230705538_AutomaticMigration"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
//... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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