首页 文章

EF6找不到LocalDBConnectionFactory

提问于
浏览
2

我有2个项目:类库(作为DAL层)和ASP.NET MVC项目(作为UI) . 为了获取数据我尝试使用EF6,但它不起作用 . 所有异常文本:

EntityFramework.dll中出现“System.InvalidOperationException”类型的异常但未在用户代码中处理附加信息:找不到具有不变名称“System.Data.SqlClient”的ADO.NET提供程序的实体框架提供程序 . 确保提供程序已在应用程序配置文件的“entityFramework”部分中注册 .

在DAL app.config中我有这个:

<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>

EF使用sqexpress提供程序生成连接字符串:

数据源= . \ SQLEXPRESS;初始目录= DAL.Entity.DataContext; Integrated Security = True; MultipleActiveResultSets = True

附:请帮助,我会讨厌这个该死的东西 .

1 回答

  • 2

    为了使您的DAL项目具有正确的配置设置,您必须在web.config中包含EntityFramework信息 . 原因是UI项目是主要程序集,因此,所有配置信息都必须来自其配置文件 . 编译时,引用的程序集配置文件不用于配置,只用于主配置文件 .

    话虽如此,这里是使用EntityFramework的 bare minimum web.configuration文件 . 确保在配置文件中正确嵌套了标记 .

    <configuration>
          <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="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DAL.Entity.DataContext;Integrated Security=True;MultipleActiveResultSets=True" />
          </connectionStrings>
          <appSettings>
            <add key="webpages:Version" value="3.0.0.0" />
            <add key="webpages:Enabled" value="false" />
            <add key="ClientValidationEnabled" value="true" />
            <add key="UnobtrusiveJavaScriptEnabled" value="true" />
          </appSettings>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
        </configuration>
    

    请注意,我并不是要复制/粘贴它,而是查看元素级别并确保在web.config中具有正确的嵌套级别 . 元素乱序将导致500.19配置错误 .

相关问题