简体   繁体   English

我应该在我的类库中包含Entity Framework还是直接包含在应用程序中?

[英]Should I include Entity Framework in my class library or directly into application?

You might have encountered this type of question before but in my case, it's a bit different scenario and I cannot find a feasible answer. 您之前可能遇到过这种类型的问题但在我的情况下,情况有点不同,我找不到可行的答案。 (It might exist already, however I was not able to find one). (它可能已经存在,但是我无法找到它)。

I'm working on a business application which has the entire functionality split into multiple class libraries. 我正在开发一个业务应用程序,它将整个功能分成多个类库。 I'm using Entity Framework and I'm not sure which is the right place to include it? 我正在使用实体框架,我不确定哪个是包含它的正确位置? In my case, I've these libraries: 就我而言,我有这些库:

  • Project.Library.Core Project.Library.Core
  • Project.Library.Models (Contains the entity data model and entities). Project.Library.Models(包含实体数据模型和实体)。
  • Project.Library.Auth (Contains authentication related and user management classes). Project.Library.Auth(包含身份验证相关和用户管理类)。
  • and similar classes who frequently use the entities and the db context. 以及经常使用实体和db上下文的类似类。

In order to make this work, I've to install Entity Framework in every library separately. 为了使这项工作,我将分别在每个库中安装Entity Framework。 Eventually they will be included in a number of ASP.NET MVC Web Applications. 最终它们将被包含在许多ASP.NET MVC Web应用程序中。 I'd have installed Entity Framework directly into the web apps but this application will have mobile apps in the future along with a possible windows store app. 我已将Entity Framework直接安装到网络应用程序中,但此应用程序将来会有移动应用程序以及可能的Windows应用程序。 In that case, I can simply set up a ASP.NET Web API and use the existing class libraries to generate the required data. 在这种情况下,我可以简单地设置ASP.NET Web API并使用现有的类库来生成所需的数据。 This is the only factor holding me back from incorporating Entity Framework directly into the web apps. 这是阻止我将Entity Framework直接纳入Web应用程序的唯一因素。

So what's your opinion on this? 那你对此有什么看法? Include Entity Framework in every library separately (required to function properly) or simply include in the web app and later follow the same approach for the Web API? 在每个库中单独包含实体框架(需要正常运行),或者只是包含在Web应用程序中,然后对Web API采用相同的方法?

Thank you. 谢谢。

You only need to "install" EntityFramework into one place: Project.Library.Models . 您只需要将EntityFramework“安装”到一个地方: Project.Library.Models Assuming standard code-first objects as your DTOs, you won't need a direct reference anywhere else as long as you wrap the context object with a Data Access Layer. 假设标准的代码优先对象作为您的DTO,只要您使用数据访问层包装上下文对象,就不需要在其他任何地方直接引用。

Adding a reference to that assembly will cause the requisite DLLs to be included with your apps, web sites, etc. Note that if you are using EntityFramework.SqlServer, the transient dependency won't be detected by Visual Studio so you need to do something like: 添加对该程序集的引用将导致必需的DLL包含在您的应用程序,网站等中。请注意,如果您使用的是EntityFramework.SqlServer,Visual Studio将无法检测到瞬态依赖项,因此您需要执行某些操作喜欢:

class Include
{
    private Include() { }
    private SqlProviderServices IncludeSql;
}

Somewhere in the models project so that DLL is copied over also. 在模型项目的某处,以便DLL也被复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类库中的实体框架7 - Entity Framework 7 in Class Library 我应该在基类上使用什么关于Entity Framework的继承策略? - What inheritance strategy on Entity Framework should I use for my base class? 引用应用程序的实体框架上下文的C#类库? - C# class library that references the application's Entity Framework context? 在不带EF软件包的应用程序中使用实体框架类库 - Using Entity Framework Class Library in the application without EF package 我应该使用类库来定位.NET Framework和C#版本? - What .NET Framework and C# version should I target with my class library? 我是否需要在实体框架模型中包括所有字段 - Do I need to include all fields in my entity framework model Entity Framework Core - 我可以直接查询子属性中的列表/对象,避免多次重复包含/然后包含吗? - Entity Framework Core - Can I query lists/objects in child property directly avoiding multiple repeating Include/ThenInclude? 类库中的实体框架为空 - Entity Framework Null in Class Library 可移植类库的实体框架 - Entity Framework for Portable Class Library 如何在类库项目中配置实体框架? - How do I configure Entity framework in class Library project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM