实体框架:未识别元素“providers”异常。

26

当我在.NET 4.0中使用Entity Framework 5.0.0时,运行时出现未识别元素'providers'异常。实际上,在使用NuGet的install-package时,加载的是Entity Framework的版本4.4.0。当我从资源管理器中检查文件属性时,可以看到:

enter image description here

这是我的配置文件:

 <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <connectionStrings>
    <add name="xxx" connectionString="metadata=res://*/StreetMusicModel.csdl|res://*/StreetMusicModel.ssdl|res://*/StreetMusicModel.msl;         provider=MySql.Data.MySqlClient;provider connection string='         server=xxx.net;         user id=xxx;         password=xxx;         database=xxx'" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity" />
    </providers>
  </entityFramework>

我有种感觉,Entity Framework 4.4.0无法识别<connectionStrings>标签。我能否直接删除或重命名<connectionStrings>部分?但是当我删除<connectionStrings>部分时,会出现另一个异常:The underlying provider failed on Open


尝试删除 , Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - Yuliam Chandra
2个回答

63

我在使用 Nuget 将 EF 从 6 版本降级到 5.0.0 版本后遇到了此问题。我认为问题是添加 EF v6 时会添加提供程序配置,但在降级后未被移除。因此,您可以只需删除 <providers> 标记内部的内容和标记本身,它就能正常工作:

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

删除对我有效。如果您删除配置文件,VS2012会重新创建它,并再次添加有问题的<entityFramework>标记,然后我必须手动将其删除。我想这是为什么呢? - timothy
回答自己的评论 - 我是从bin目录中的.exe.config文件中删除的,而不是从App.config文件中删除的。从App.config文件中删除即可解决问题。 - timothy
1
@timothy 这是程序员经常犯的那种三倍脸掌错误之一。而我今天刚刚犯了这个错误...不知道我的头脑在哪里。 - Apostrofix
这正是我的情况。我通过Nuget从EF 6降级到了5。感谢您的答案! - YeahStu
谢谢你的答复。从5升级到6破坏了OData服务,降级回到5会生成这个问题。 - Hong

0
你需要从项目的 app.config 文件中删除 <startup> 标签,而不是从 YourProjectName.dll.config 文件中删除,因为在构建项目后它会自动从 YourProjectName.dll.config 中删除。
注意:答案已经在上面的评论中提到了,我只是将答案结合在这里。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接