【问题标题】:Entity Framework Code First migration issues -- Dataloss实体框架代码优先迁移问题——数据丢失
【发布时间】:2014-07-22 21:52:04
【问题描述】:

我已阅读有关代码优先迁移的所有博客文章和 MSDN 文章 (http://msdn.microsoft.com/en-us/data/jj591621.aspx),但我不太清楚应该如何使用它。

这是我项目的迁移历史:

  1. 最初我使用Enable-Migrations,然后是Add-MigrationUpdate-Database
  2. 我部署了项目
  3. 我对模型做了一些小的改动。重新运行 add-migrationupdate-database
  4. 部署项目
  5. 我为模式添加了更多属性。另外,我运行Disable-Migrations 并运行Enable-Migrations -EnableAutomaticMigration
  6. 现在,当我部署项目并第一次运行应用程序时,所有现有数据都消失了

旧项目(第 4 步)- Migrations\Configurations.cs

namespace POC_Manager.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<POC_Manager.Models.POC_ManagerContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            ContextKey = "POC_Manager.Models.POC_ManagerContext";
        }

        protected override void Seed(POC_Manager.Models.POC_ManagerContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}

新项目(第 6 步)- Migrations\Configurations.cs

namespace POC_Manager.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<POC_Manager.Models.POC_ManagerContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            ContextKey = "POC_Manager.Models.POC_ManagerContext";
        }

        protected override void Seed(POC_Manager.Models.POC_ManagerContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}

旧项目(第 4 步)——Get-Migrations 的输出

PM> 获取迁移 检索已应用于目标数据库的迁移。 201405271907443_AutomaticMigration 201404252210039_InitialCreate

新项目(第 6 步)——Get-Migrations 的输出

PM> 获取迁移 检索已应用于目标数据库的迁移。 201407022020263_AutomaticMigration 201406262227296_AutomaticMigration 201405271907443_AutomaticMigration 201404252210039_InitialCreate 下午>

另一个令人困惑的部分是......启用自动迁移后我还需要运行 Update-Database 命令吗??

【问题讨论】:

    标签: asp.net-mvc entity-framework asp.net-mvc-4


    【解决方案1】:

    自动迁移用于根据类的更改自动生成迁移文件;您仍然需要运行 Update-Database,除非您将其逻辑构建到初始化策略中。

    就您的数据丢失而言,这很可能是基于您使用的初始化策略。我建议您坚持使用 CreateDatabaseIfNotExists,除非您的项目确实需要自定义初始化程序;其他标准的在(早期)开发环境之外并不是非常有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多