实体框架5有代码优先连接字符串错误



好吧,我已经在谷歌上搜索了一早上了,我真的需要一些帮助。我正在读亚当·弗里曼(专业ASP)写的一本书。Net MVC 4),我被困在第7章。顺便说一句,我不知道为什么Apress没有像Wrox这样的支持论坛,在那里作者可以帮助人们摆脱他们书中的例子。

无论如何,这本书首先使用了一个数据库到EF,根据这本书,我创建了一个localDB,定义了DB模式并添加了一些示例数据。然后创建这个DBcontext

    using System.Data.Entity;
    using SportsStore.Domain.Entities;
    namespace SportsStore.Domain.Concrete
   {
       class EFDbContext : DbContext
   {
       public DbSet<Product> Products { get; set; }
   }
   }

然后这里是连接字符串

<connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=(localdb)v11.0;Initial     Catalog=SportsStore;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>

还有,这里有一些设置我猜是EF/Nuget在安装时自动添加的

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>

错误信息到处都是,因为我一直在搞乱它,错误信息不断变化,但它们都指向实体框架的一些东西。请帮忙,任何帮助都是非常感谢的,这样我就可以继续我的自学了。

当前错误消息是"配置节'entityFramework'无法读取,因为它缺少一个节声明"

Config Source:
   96:   </runtime>
   97:   <entityFramework>
   98:     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">

要尝试处理错误,您可以在构造函数中指定您的连接字符串名称:

using System.Data.Entity;
using SportsStore.Domain.Entities;
namespace SportsStore.Domain.Concrete
{
    public class EFDbContext : DbContext
    {
        public EFDbContext() : base("EFDbContext") {}
        public DbSet<Product> Products { get; set; }
    }
}

确保传递给名称的字符串与"名称"匹配。属性设置在web.config

<connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=(localdb)v11.0;Initial     Catalog=SportsStore;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

如果这不起作用,请尝试使用"name="另外,如下(此处有用的参考)。这将强制EF5抛出一个错误,如果它在配置文件中没有找到连接字符串,您可以使用该错误进行诊断。

namespace SportsStore.Domain.Concrete
{
    public class EFDbContext : DbContext
    {
        public EFDbContext() : base("name=EFDbContext") {}
        public DbSet<Product> Products { get; set; }
    }
}

如果这不起作用,那么我们需要您提供一些异常详细信息。

编辑:

"配置节'entityFramework'无法读取,因为它缺少一个节声明"

你的entityFramework部分应该是这样的,注意它是元素的直接子元素:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- other section and sectionGroup declarations -->
  </configSections>
  <!-- other sections -->
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <!-- other sections -->
</configuration>

我使用ASP。. NET Core 1.0 RC1对我来说,它没有工作,因为web.config。问题出在web上。配置文件。在开始时,我的配置文件看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
    </handlers>
    <httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600"/>
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

解决这个问题有两种方法。首先是添加到web中。如Andy Brown所示。注意不同版本的EF。

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
</configuration>

第二个是删除整个entityFramework部分

最新更新