洋葱架构项目中的实体框架

时间:2014-11-18 18:13:10

标签: c# asp.net-mvc entity-framework asp.net-web-api entity-framework-6

我有一个使用Ninject作为IoC的ASP.NET WebAPI 2项目。我有一个核心层,我的所有类库项目都使用它,并定义了通用接口。

我有一个名为Repositories的层,其中我放置了我的应用程序存储库,以及一个名为DataLayer的层,它定义了不同的Entity Framework DbContext对象。我的存储库图层引用了我的DataLayer,在DataLayer和存储库图层上我添加了EntityFramework。

在存储库层中分类的所有存储库都是从核心实现公共接口,并使用ninject注入到API中的控制器。首先,当我运行我的项目并尝试对存储库执行某些操作时,我收到以下错误消息:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

我接下来要做的是打开存储库层中的app.config文件,并将EntityFramework防御复制到API项目中的web.config:

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

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

但现在我收到以下错误:

An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code

Additional information: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

我不认为解决方案是在API引用中包含实体框架,因为这会破坏关注点的分离 - API不应该关心存储库使用什么技术来访问数据 - 所以如果我想在一个月内说我想改变我的ORM甚至是我的数据库,那么API就不应该关心我使用什么和如何使用,只要它使用相同的接口。

那么问题是什么?我是EF的新手,所以我认为我必须遗漏一些东西......

3 个答案:

答案 0 :(得分:0)

你的Web Config肯定搞砸了。我会尝试删除.edmx并尝试添加一个新的.edmx。这就是我的配置的样子,不确定它是否有帮助。

 <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.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" /> //did you change this here?
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

答案 1 :(得分:0)

  

API不应该关心存储库使用什么技术来访问数据

您有一个应用程序配置文件和一个AppDomain。两者都需要加载相关的Entity Framework配置和程序集,因为DAL中的代码需要它们。

您的网络API的代码应该不关心,但应用程序配置和托管环境是独立的。

答案 2 :(得分:0)

请尝试从Package Manager控制台再次安装Entity框架:

PM> Install-Package EntityFramework

这可以解决您的问题,因为我曾经遇到过同样的问题。 希望这会成功。 感谢