简体   繁体   English

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

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

I seem to be getting problems using the basic features of enable-migrations and update-database. 我似乎在使用enable-migrations和update-database的基本功能时遇到了问题。 The project I have has migrations already enabled before I copied it over to a different PC. 在将项目复制到另一台PC之前,我已经启用了项目。

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

In my Configuration class I have: 在我的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

In my web.config, I have EF setup to point to a database Recruitment. 在我的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>

Why would my database get created by the name of RecruitDB.ModelContext.RecruitModelContext? 为什么我的数据库是通过RecruitDB.ModelContext.RecruitModelContext的名称创建的?

Is there some way to stop this from happening? 有没有办法阻止这种情况发生?

I try to reset by using Enable-Migrations -Force and re-writing my Configuration file after it gets overwritten. 我尝试使用Enable-Migrations -Force重置,并在覆盖后重写我的配置文件。

Sometimes I get my correctly named Recruitment database and start using the application fine. 有时我会得到正确命名的Recruitment数据库并开始正常使用该应用程序。 Subsequently, I cannot get the Seed method to run. 随后,我无法运行Seed方法。

I end up having to delete the database and start over. 我最终不得不删除数据库并重新开始。

I assume you are using EF Code First? 我假设您使用EF Code First?

It seems that EF is defaulting to "Code First connection by convention", which explains why the fully qualified name of the DbContext is used as the database name. 似乎EF默认为“按惯例的代码优先连接”,这解释了为什么DbContext的完全限定名称用作数据库名称。 (Read more on this here: http://msdn.microsoft.com/en-us/data/jj592674 ) (在这里阅读更多内容: http//msdn.microsoft.com/en-us/data/jj592674

Firstly, try to set the name of the connection string to the fully qualified name of the DbContext class, namely "RecruitDB.ModelContext.RecruitModelContext". 首先,尝试将连接字符串的名称设置为DbContext类的完全限定名称,即“RecruitDB.ModelContext.RecruitModelContext”。

Should this fail, try the following steps: 如果此操作失败,请尝试以下步骤:

  1. Drop the database RecruitDB.ModelContext.RecruitModelContext 删除数据库RecruitDB.ModelContext.RecruitModelContext
  2. Delete all migrations in the migrations folder (Delete the entire migrations folder) 删除迁移文件夹中的所有迁移(删除整个迁移文件夹)
  3. Run the Enable-Migrations command 运行Enable-Migrations命令
  4. Run the Add-Migration command with the ConnectionStringName parameter specified 运行Add-Migration命令并指定ConnectionStringName参数
  5. Run the Update-Database command 运行Update-Database命令

The following is a good EF Migrations Command Reference: http://coding.abel.nu/2012/03/ef-migrations-command-reference/#Enable-Migrations 以下是一个很好的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