引用EntityFramework.dll会导致VS2010中出现运行时错误

时间:2013-11-27 14:31:02

标签: c# visual-studio-2010 entity-framework ef-code-first runtime-error

[更新]

我的解决方案中有2个项目。第一个是我的DAL项目(类库),第二个是Windows项目(UI),我的DAL项目引用了EF 4.3.1并正常工作, 在EF official site中应用建议的更改之后,最近我在VS2010中使用程序包管理器控制台EF6引用了我的DAL项目Update-Package EntityFramework,现在是我的DAL项目的 app.config 包括以下几行:

<configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="Data Source=MyServer;Initial Catalog=erptest;Persist Security Info=True;User ID=sa_l4;password=12212121;application name = LEVEL4" />
  </parameters>
</defaultConnectionFactory>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

但是当我运行我的项目时,控制权来到线下

var context = new MyDbContext();

我收到了这个错误:

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.Odbc'. Make sure the provider is registered in the 'entityFramework' section of the application config file

有谁知道,问题出在哪里?

2 个答案:

答案 0 :(得分:4)

由于您的DAL项目是类库,它将使用消耗它的应用程序的配置。

尝试将Entity Framework Nuget包添加到UI项目中。它会将所需的设置添加到配置中。

<强>更新

此问题也可能由Glimpse或EF Profiler等分析工具引起。如果您使用其中任何一种,请尝试禁用它们以查看它是否能解决问题。

答案 1 :(得分:1)

听起来像配置问题...您的web.config文件中是否有以下条目?

<entity-framework>
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entity-framework>

您的特定配置条目可能需要一些额外/不同的条目/属性,但您可以在此处阅读有关EF6配置的更多信息:

http://msdn.microsoft.com/en-us/data/jj556606.aspx