繁体   English   中英

ASP.NET MVC 4和EF5:如何使用enable-migrations / update-database

[英]ASP.NET MVC 4 and EF5: how to use enable-migrations / update-database

我似乎在使用enable-migrations和update-database的基本功能时遇到了问题。 在将项目复制到另一台PC之前,我已经启用了项目。

我使用的是VS 2010 Ultimate,SQL Server 2008 R2 Express,MVC 4和EF5。

在我的Configuration类中,我有:

public Configuration()
{
    AutomaticMigrationsEnabled = true;
}

protected override void Seed(RecruitDB.ModelContext.RecruitModelContext context)
{
    context.Statuses.AddOrUpdate(
        s => s.ProgressStatus,
        new Status { ProgressStatus = "Interview Arranged" },
        new Status { ProgressStatus = "Candidate Rejected" },
        new Status { ProgressStatus = "Candidate Accepted" },
        new Status { ProgressStatus = "Job Offer Made" },
        new Status { ProgressStatus = "Job Offer Accepted" },
        new Status { ProgressStatus = "Job Offer Declined" }
        );
    // etc

在我的web.config中,我有EF设置指向数据库Recruitment。

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
<connectionStrings>
    <add name="RecruitModelContext" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Initial Catalog=Recruitment;Integrated Security=True;MultipleActiveResultSets=true" />
</connectionStrings>

为什么我的数据库是通过RecruitDB.ModelContext.RecruitModelContext的名称创建的?

有没有办法阻止这种情况发生?

我尝试使用Enable-Migrations -Force重置,并在覆盖后重写我的配置文件。

有时我会得到正确命名的Recruitment数据库并开始正常使用该应用程序。 随后,我无法运行Seed方法。

我最终不得不删除数据库并重新开始。

我假设您使用EF Code First?

似乎EF默认为“按惯例的代码优先连接”,这解释了为什么DbContext的完全限定名称用作数据库名称。 (在这里阅读更多内容: http//msdn.microsoft.com/en-us/data/jj592674

首先,尝试将连接字符串的名称设置为DbContext类的完全限定名称,即“RecruitDB.ModelContext.RecruitModelContext”。

如果此操作失败,请尝试以下步骤:

  1. 删除数据库RecruitDB.ModelContext.RecruitModelContext
  2. 删除迁移文件夹中的所有迁移(删除整个迁移文件夹)
  3. 运行Enable-Migrations命令
  4. 运行Add-Migration命令并指定ConnectionStringName参数
  5. 运行Update-Database命令

以下是一个很好的EF迁移命令参考: http//coding.abel.nu/2012/03/ef-migrations-command-reference/#Enable-Migrations

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM