entity-framework - 使用 EF6.x.x Code First 不迁移的缺点

标签 entity-framework entity-framework-6

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

最佳答案

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

1)案例:DbMigrationsConfiguration 自动:

[__MigrationHistory] ​​跟踪上次迁移。

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

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

2) 案例:DbMigrationsConfiguration 非自动:

[__MigrationHistory] ​​跟踪每次迁移。
  this.AutomaticMigrationsEnabled = false;
  this.AutomaticMigrationDataLossAllowed = false;

每个迁移级别获得一个migrationId 标识符,用于向上/级别/向下迁移级别。

最佳实践:如果您想使用自动迁移,只需重新生成迁移并发送给客户。

如果您使用的是非自动迁移。
1)如果客户需要数据,那么只需将数据与 SQL 转换为新的数据库模式
2)如果客户不需要数据,那么只需删除数据库并使用initialCreate再次创建它。

如果要创建没有任何迁移历史信息的数据库:
        // 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();
        }

我希望这能帮助您解决问题!

如果你真的想要更少的 Db-Schema,那么使用 NoSql 和 EF。
在 Core 版本中仍未完成,但在旧版本 EF6 中,可以使用以下命令 (NoSql Db):http://www.brightstardb.com或使用微软的 Polyglot appoarch https://msdn.microsoft.com/en-us/library/dn271399.aspx

关于entity-framework - 使用 EF6.x.x Code First 不迁移的缺点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37695220/

相关文章:

c# - 删除每种 Entity Framework 的前 10 名以外的所有内容

c# - 组合框不会加载数据库值

ef-code-first - 我如何将 Sparx Enterprise Architect 与 Code First 一起使用?

c# - Entity Framework code first - 不是为了创建表吗?

c# - 如何在 Entity Framework 查询中初始化一个空列表?

c# - Entity Framework 中不同项目之间 EntityTypeConfiguration 的继承

c# - 检测 Entity Framework 4.0 中 ObjectSet 的变化?

c# - 在 Entity Framework Core 中重用记录

entity-framework - Entity Framework : join on the rule "A = substring(B)"?

c# - Entity Framework - 同一实体的不同代理对象。并包括具有多条路径到同一目的地的行为