【发布时间】:2014-07-22 21:52:04
【问题描述】:
我已阅读有关代码优先迁移的所有博客文章和 MSDN 文章 (http://msdn.microsoft.com/en-us/data/jj591621.aspx),但我不太清楚应该如何使用它。
这是我项目的迁移历史:
- 最初我使用
Enable-Migrations,然后是Add-Migration和Update-Database。 - 我部署了项目
- 我对模型做了一些小的改动。重新运行
add-migration和update-database。 - 部署项目
- 我为模式添加了更多属性。另外,我运行
Disable-Migrations并运行Enable-Migrations -EnableAutomaticMigration - 现在,当我部署项目并第一次运行应用程序时,所有现有数据都消失了
旧项目(第 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