Asp.net mvc 5 代码优先迁移时出现实体框架错误

Asp.net mvc 5 代码优先迁移时出现实体框架错误,asp.net-mvc-5,entity-framework-6,entity-framework-migrations,localdb,Asp.net Mvc 5,Entity Framework 6,Entity Framework Migrations,Localdb,使用Package Manager控制台Visual Studio 2013为LocalDb数据库发出添加迁移命令时发生此错误: 从数据库获取提供程序信息时出错。这可能是由于实体框架使用了不正确的连接字符串造成的。检查内部异常以了解详细信息,并确保连接字符串正确 这是一个使用EntityFramework6.0.0的MVC5项目 以下是web.config中特定于EF的部分: <configSections> <!-- For more information on Entit

使用Package Manager控制台Visual Studio 2013为LocalDb数据库发出添加迁移命令时发生此错误:

从数据库获取提供程序信息时出错。这可能是由于实体框架使用了不正确的连接字符串造成的。检查内部异常以了解详细信息,并确保连接字符串正确

这是一个使用EntityFramework6.0.0的MVC5项目

以下是web.config中特定于EF的部分:

<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="Application" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-XXXXManagement-20141105035546.mdf;Initial Catalog=aspnet-XXXXManagement-20141105035546;Integrated Security=True;MultipleActiveResultSets=True"
  providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
  </parameters>
</defaultConnectionFactory>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>

我可以添加迁移并更新数据库。但是,当我尝试添加另一个迁移时,会显示错误。我可以通过打开web.config文件并保存它(ctrl-s)来解决此问题。然后我可以再添加一个迁移

为什么会这样

背景:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

using OIVSManagement.Model;

namespace OIVSManagement.Database
{
  public class ApplicationContext : DbContext
  {
    public DbSet<Organization> Organizations { get; set; }
  }
}
使用系统;
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Linq;
使用System.Web;
使用OIVSManagement.Model;
命名空间OIVSManagement.Database
{
公共类应用程序上下文:DbContext
{
公共数据库集组织{get;set;}
}
}
组织模式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OIVSManagement.Model
{
    public class Organization
    {
      public virtual int Id { get; set; }

      public virtual string Name { get; set; }

      public virtual List<Contact> Contacts { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间OIVSManagement.Model
{
公营班级组织
{
公共虚拟整数Id{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟列表联系人{get;set;}
}
}
联系方式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OIVSManagement.Model
{
  public class Contact
  {
    public virtual int Id { get; set; }

    public virtual Organization Organization { get; set; }

    public virtual ContactCategory Category { get; set; }

    public virtual string FirstName { get; set; }

    public virtual string LastName { get; set; }

    public virtual List<EmailAddress> EmailAddresses { get; set; }
  }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间OIVSManagement.Model
{
公共类联系人
{
公共虚拟整数Id{get;set;}
公共虚拟组织{get;set;}
公共虚拟联系人类别{get;set;}
公共虚拟字符串FirstName{get;set;}
公共虚拟字符串LastName{get;set;}
公共虚拟列表电子邮件地址{get;set;}
}
}

我很好奇:当您保存web.config文件时,
AttachDbFilename
的连接字符串中的值是否与迁移尝试之前的值不同?不,我所做的只是打开web配置(在VS2013中),点击ctrl-s,然后关闭它。然后迁移就开始了。我的意思是,你看到了什么不同吗?连接字符串有什么变化吗?我很难将项目恢复到可以测试的程度,但现在已经完成了。答案是否定的,连接字符串完全相同。我更新了运行的数据库,然后向模型类添加了属性,并尝试添加迁移,这导致了上面显示的相同错误。我打开了web配置,添加了一个空行,并保存了它。然后迁移开始了。我在项目中使用git,包括web配置。除了添加的空行外,该文件没有显示任何更改。我也有同样的问题,在web.config中添加一个空行也为我解决了这个问题。我真的很高兴你提到这一点!