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

本文关键字:实体 框架 项目 洋葱 | 更新日期: 2023-09-27 18:30:57

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

我有一个名为存储库

的层,我在其中放置我的应用程序存储库,还有一个名为 DataLayer 的层,它定义了不同的实体框架 DbContext 对象。我的存储库层引用了我的数据层,并且在数据层和存储库层上,我都添加了实体框架。

所有分类在存储库层内的存储库都从核心实现通用接口,并使用 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 的新手,所以我想我一定错过了什么......

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

你的 Web 配置肯定在某个地方搞砸了。我会尝试删除 .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>

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

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

Web API 的代码不应在乎,但应用程序配置和托管环境与此是分开的。

请尝试从包管理器控制台再次安装实体框架:

PM> Install-Package EntityFramework

这应该可以解决您的问题,因为我曾经遇到过同样的问题。希望这能解决。谢谢