2014-07-22 28 views
0

我已阅读关于代码第一次迁移的每篇博文和MSDN文章(http://msdn.microsoft.com/en-us/data/jj591621.aspx),但我不清楚该如何使用它。实体框架代码第一次迁移问题 - Dataloss

这里是我的项目迁移历史:

1.Initially我用“启用的迁移”,然后添加迁移和更新数据库。 2.我部署了项目 3.我对模型做了一些小改动。重新运行添加迁移和更新数据库。 4.部署项目 5.我增加了更多的属性模式。另外,我运行禁用的迁移和运行Enable-迁移-EnableAutomaticMigration 6.现在,当我部署项目..和运行的第一次,所有现有数据已经​​一去不复返了

老项目的应用程序(步骤#4) - 迁移\ 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) - 迁移\ 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-迁移输出

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

新建项目(步骤#6) - 从获取的迁移输出

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

另一个令人困惑的是,...我仍然需要运行启用自动迁移后更新的数据库的命令?

回答

1

自动迁移是根据您的类的变化自动生成迁移文件;您仍然需要运行更新数据库,除非您将逻辑构建到初始化策略中。

就数据丢失而言,它很可能基于您使用的初始化策略。我建议你坚持使用CreateDatabaseIfNotExists,除非你的项目真的需要一个自定义初始化器;除了(早期)开发环境以外,其他标准并不是非常有用。

相关问题