[英]Entity Framework 6 - How to update customer's database with CodeFirst migration
My team develops an application which deploys MSSQL database at customer's system. 我的团队开发了一个在客户系统上部署MSSQL数据库的应用程序。 We encountered a problem with using migrations to update the customer's database structure .
我们在使用迁移来更新客户的数据库结构时遇到问题。
We can't use automated migrations because more than one instance of the app can run on the same database so if one of instances gets updated and therefore changes the model and therefore the structure of database the others change it back so neither of them can work on the database. 我们不能使用自动迁移,因为一个应用程序的一个以上实例可以在同一个数据库上运行,因此如果其中一个实例被更新并因此更改了模型并因此更改了数据库的结构,则其他实例又将其改回,因此它们都不起作用在数据库上。
We can't use nonautomated migrations because we have no access to customer's database to run the update-database command. 我们不能使用非自动迁移,因为我们无权访问客户的数据库来运行update-database命令。
The question is what's the best approach to keep the database and the model always up to date on the level of code ? 问题是使数据库和模型始终保持最新的代码水平的最佳方法是什么?
You have to use the migrate to latest version strategy. 您必须使用“迁移到最新版本”策略。 This apporach allows you to automatically update the database when the model is changed:
使用此方法,可以在更改模型时自动更新数据库:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, MyMagicDatabaseConfiguration>()`);
public class MyMagicDatabaseConfiguration : DbMigrationsConfiguration<MyDbContext>
{
public MyMagicDatabaseConfiguration()
{
this.AutomaticMigrationsEnabled = true;
this.AutomaticMigrationDataLossAllowed = true;
}
}
// !!Force the initialization. this will execute the update!!
MyDb.Context.Database.Initialize(true);
This works fine if you are using MS SQL Server 如果您使用的是MS SQL Server,则可以正常工作
The problem you are using MySql you have to do everything by your self!: 您使用MySql的问题是您必须自己做所有事情!:
var migrator = new DbMigrator(new DbMigrationsConfiguration ());
migrator.Update();
// In the migrtaions directory:
public partial class MyMigration : DbMigration
{
public override void Up()
{
AddColumn("dbo.MyTable", "AnyName", c => c.Boolean(nullable: false));
}
public override void Down()
{
DropColumn("dbo.MyTable", "AnyName");
}
}
This is not an easy work and I do not recommeded you to do it. 这不是一件容易的事,我不建议您这样做。 Just use SQL Server apporach this will safe your time.
只需使用SQL Server方法,这将节省您的时间。 one note more: Magic migration sometimes does not working(Complex changes with keys), if the changes can not be handled automatically.
还有一点需要注意:如果无法自动处理更改,则魔术迁移有时不起作用(使用键进行复杂更改)。
You can also use migration to a target version: 您还可以使用迁移到目标版本:
var configuration = new DbMigrationsConfiguration();
var migrator = new DbMigrator(configuration);
migrator.Update("HereMigrationId");
var scriptor = new MigratorScriptingDecorator(migrator);
var migrationScript = scriptor.ScriptUpdate(sourceMigration: null, targetMigration: "HereMigrationId");
In your case you need migration to target version. 在您的情况下,您需要迁移到目标版本。 with the decrator you can modifiy the migration script during the migration.
使用declator,您可以在迁移期间修改迁移脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.