使用EF 6.x.x代码的优点,无需迁移 [英] Dis-Advantages of using EF 6.x.x Code First without Migration

查看:139
本文介绍了使用EF 6.x.x代码的优点,无需迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我厌倦了使用/运行 Add-Migration Update-Database ,因为很多时候我们忘记了在生产数据库上运行迁移。因此,我们决定删除数据库表中的所有迁移以及所有迁移类。那么,如果我的__MigrationHistory表始终是空的,没有迁移类呢?主要缺点是什么?

解决方案

不,这是一个坏主意,[__MigrationHistory]用于将您当前使用的概念模型与存储模型进行比较。



1)案例:DbMigrationsConfiguration automatic:



[__ MigrationHistory]跟踪最后一次迁移。

  this.AutomaticMigrationsEnabled = true; 
this.AutomaticMigrationDataLossAllowed = true;

$
如上面的图像,migrationId是迁移标识符,模型列是当前概念模型的二进制流的base64表示。



2)情况:DbMigrationsConfiguration非自动:



[__ MigrationHistory]跟踪每次迁移。

  .AutomaticMigrationsEnabled = false; 
this.AutomaticMigrationDataLossAllowed = false;

每个迁移级别都获得一个用于迁移级别升级/升级/降级的迁移标识符。 p>

最佳做法:如果要使用自动迁移,只需重新生成迁移并发送给客户。



如果您正在使用非自动迁移。
1)如果客户需要数据,那么只需用SQL将数据转换到新的数据库模式
2)如果客户不需要数据,那么只需删除数据库就可以用initialCreate重新创建它。






如果要创建没有任何migrationHistory信息的数据库:

  //创建数据库。您可以从SMO脚本数据库生成它,如果数据库存在,那么只需忽略创建
//不要忘记删除[__MigrationHistory]行。
DbContext.Database.ExecuteSqlCommand(SQLCreateDbScript.sql);

Database.SetInitializer< MyDbContext>(null);

使用(var dbContext = new MyDbContext())
{
//创建DbContext,它可以工作!
dbContext.Users.Add(new User());
dbContext.SaveChanges();
}

我希望这将有助于您解决问题!



如果你想要一些真正的Db-Schema,那么使用带有EF的NoSql。
在核心版本中仍然没有完成,但使用旧版本EF6可以通过以下方式(NoSql Db): http ://www.brightstardb.com 或使用微软的Polyglot appoarch https://msdn.microsoft.com/en-us/library/dn271399.aspx


I am fed up of using/running Add-Migration and Update-Database because lot of time we forget to run migrations on production database. So, we decided to delete all migrations from database table as well all migration classes. So, what if my __MigrationHistory table remains always empty and no migration classes. What is the main disadvantage?

解决方案

No this is a bad idea, the [__MigrationHistory] is used to compare your current used conceptual model with the storage model.

1) Case: DbMigrationsConfiguration automatic:

[__MigrationHistory] keeps track for the last migration.

  this.AutomaticMigrationsEnabled = true;
  this.AutomaticMigrationDataLossAllowed = true;


as shown in the image above, the migrationId is the migration identifier and the model column is a base64 representation of the binary stream of your current conceptual model.

2) Case: DbMigrationsConfiguration non-automatic:

[__MigrationHistory] keeps track of every migration.

  this.AutomaticMigrationsEnabled = false;
  this.AutomaticMigrationDataLossAllowed = false;

Each migration level obtain a migrationId identifier which is used for migration level up/level/down.

Best practice: If you want to use the automatic migration just regenerate the migration and shipped to the custmer.

If you are using non-automatic migration. 1) If the customer need the data then just tranform the data with SQL to the new db schema 2) If the customer does not need the data then just drop the database an create it again with initialCreate.


If you want to create the database without any migrationHistory info:

        // Create the database. You can generate it from SMO "Script database as" and if the database exist then just ignore the creation
        // Do not forget to remove the [__MigrationHistory] rows.
        DbContext.Database.ExecuteSqlCommand("SQLCreateDbScript.sql");

        Database.SetInitializer<MyDbContext>(null);

        using (var dbContext = new MyDbContext())
        {
            // Create DbContext and it works!
            dbContext.Users.Add(new User());
            dbContext.SaveChanges();
        }

I hope this will help you to solve the problem!

And if you want something really with less Db-Schema then use NoSql with EF. In Core version still not done but with the old version EF6 it is possible todo that (NoSql Db) with: http://www.brightstardb.com or use the Polyglot appoarch from microsoft https://msdn.microsoft.com/en-us/library/dn271399.aspx

这篇关于使用EF 6.x.x代码的优点,无需迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆