1

好吧,我整个早上都在谷歌上搜索,我真的可以使用一些帮助。我正在关注 Adam Freeman(Pro 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">
4

2 回答 2

3

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

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 在配置文件中找不到连接字符串,这应该会强制 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>
于 2013-06-04T17:00:05.660 回答
0

我使用了 ASP.NET Core 1.0 RC1。对我来说,由于 web.config,它不起作用。问题出在 web.config 文件上。一开始我的配置文件是这样的:

<?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.config 行,如 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 部分。

于 2016-03-28T12:33:15.903 回答