简体   繁体   English

实体框架6-如何使用CodeFirst迁移更新客户的数据库

[英]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.

相关问题 如何使用Entity Framework 4.2执行数据库迁移? - How can I perform database migration using Entity Framework 4.2? 如何在实体框架中更新实体? - How to update an entity in Entity Framework? 如何在数据库插入和更新上调用函数? 还使用实体框架吗? - How to call a function on database insert and update? Also using Entity Framework? 实体框架6.0代码优先迁移-模型/数据库兼容性错误? - Entity Framework 6.0 Code First Migration - Model/Database compatibility bug? 实体框架代码首先使用现有数据库迁移策略 - Entity framework code first migration strategy with existing database 没有默认数据库时的Entity Framework 6和迁移 - Entity Framework 6 and migration when I don't have a default Database 使用Entity Framework在运行时更新数据库模式 - Update database schema at runtime with Entity Framework 使用MVC Codefirst中的迁移脚本更改生产数据库 - Changing Production database using Migration Script in MVC Codefirst 如何处理.NET MVC实体框架迁移中的创建和修改日期? - How to handle Creation & Modified Date in .NET MVC Entity Framework Migration? 如何通过 ADO.NET 实体框架从数据库更新记录? - How to update a record from database via ADO.NET Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM