执行代码优先迁移在发布设置中显示为灰色

时间:2012-09-19 13:30:03

标签: asp.net-mvc-3 entity-framework azure

使用Windows Azure并尝试发布我的MVC3应用程序。 “发布”Web应用程序的“设置”面板中的“执行代码优先迁移”复选框显示为灰色。我需要做些什么改变才能启用它?

2 个答案:

答案 0 :(得分:8)

我相信当您尝试发布MVC应用程序时,您会看到以下“执行代码优先迁移”:

enter image description here

这可能是因为您没有为应用程序中的代码迁移编写完整代码,也没有按照here所述的web.config中没有或不正确的数据库设置。

为了启用代码迁移,您必须在web.config中配置一个数据库(如果是Windows Azure,则需要在web.config中提供SQL数据库信息),并在代码中编写完整的类迁移将取决于您的模型。 Here是如何实现它的一个例子。

http://msdn.microsoft.com/en-us/library/dd394698#efcfmigrations

答案 1 :(得分:0)

我假设您已经拥有实体框架模型并且已经在您的数据库中(如果没有,那么您需要做一些阅读,@AvkashChauhan回答确实是一个很好的起点)。

但是,如果你有一个模型和所有配置,如:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Configurations.Add(new YourEntityMap());
}

和所有实体映射如:

public class YourEntityMap : EntityTypeConfiguration<YourEntity>
{
    public YourEntityMap()
    {
        this.HasKey(t => t.Id);
    }
}

并且您仍然没有启用该darn复选框,您可能希望执行以下步骤:

转到Tools&gt; NuGet Package Manager&gt; Package Manager Console

enter image description here

然后在控制台写

Enable-Migrations -ContextTypeName Company.Models.YourDevContext

其中Company.Models.YourDevContext是您的数据库上下文(查找继承自DbContext的类应与具有OnModelCreating覆盖的类相同。)

运行命令后,你应该得到类似的东西:

enter image description here

此时,您应该在解决方案more on how to handle migrations here

中添加Migrations文件夹

希望这可以节省你一些时间。