简体   繁体   English

EF6:必须包含在启动项目中吗?

[英]EF6: Have to include in startup project?

I have three projects: Persistence, Logic and Test. 我有三个项目:持久性,逻辑和测试。

The Persistence project has EF specific stuff. Persistence项目具有EF特定的东西。 The Logic project makes use of a Repository pattern to interact with the Persistence/EF stuff. Logic项目利用Repository模式与Persistence / EF进行交互。 The Test project is a generic console that calls up a Logic object and checks that my EF setup is correct. Test项目是一个通用控制台,它调用Logic对象并检查我的EF设置是否正确。 Normal stuff. 正常的东西。

In order to run this, I apparently need to have EF installed on the Test project (and I need to move/copy my App.Config from the EF class lib project to the Test project). 为了运行它,我显然需要在Test项目上安装EF(我需要将我的App.Config从EF类lib项目移动/复制到Test项目)。

It's fine, it works, but when I get into other component/application layer projects, I don't want to have to include an entire EF reference for these "entry points" that don't ever deal with EF directly. 它很好,但是当我进入其他组件/应用程序层项目时,我不希望必须为这些直接处理EF的“入口点”包含整个EF引用。

How to keep EF contained specifically to its "own" project? 如何将EF专门保存到其“自己的”项目中? Thanks 谢谢

edit for possible duplicate: the possible duplicate deals with how to decouple the business logic from the DAL. 编辑可能的重复:可能的重复处理如何将业务逻辑与DAL分离。 My question deals with why the presentation project requires a reference to Entity Framework: I already have the BAL/DAL segregated and a repository pattern in place. 我的问题涉及为什么演示项目需要引用实体框架:我已经将BAL / DAL隔离并且存储库模式已经到位。

Ideally you should limit the dependency of the Entity framework frame work to the minimum amount as possible. 理想情况下,您应该将实体框架框架工作的依赖性限制为尽可能小的数量。 The best way to do it is to use EF only in the layer /Project where you have the data access code (Your Data access layer). 最好的方法是仅在具有数据访问代码的层/项目中使用EF(您的数据访问层)。 It is better if you return DTO'/POCO's (Not Entities created by EF) to whoever calling those Data Access methods (Usually from Your Business Layer). 如果您将DTO'/ POCO(由EF创建的非实体)返回给那些调用这些数据访问方法(通常来自您的业务层)的人,则会更好。 You may keep this DTO's in a seperate project ( Let's call it YourProject.Common ). 您可以将此DTO保存在单独的项目中(我们称之为YourProject.Common )。 With this approach, you can always switch your DAL layer to use another data access approach (NHibernate , Pure ADO.NET etc..). 使用这种方法,您始终可以切换DAL层以使用其他数据访问方法(NHibernate,Pure ADO.NET等)。 If you are using Entity framework, You still need to keep the Connection string (for EF, with all those metadata stuff :( ) in your startup project. 如果您正在使用Entity框架,您仍需要在启动项目中保留Connection字符串(对于EF,以及所有这些元数据:())。

So your Layers/Projects will be something like this 所以您的图层/项目将是这样的

  • Common ( Will have all the DTO/POCO's) 共同 (将拥有所有DTO / POCO)
  • DataAccess ( Will have the Data access code, This refer the YourProject.Common Project and return DTOs from that project. You don't need to keep the connection string in this project for your app to run as this will be called always from the startup project, which has the connection string. But, keep the App.Config (Generated by EF) as it is because you need that when you update your EDMX files later for db changes. DataAccess (将具有数据访问代码,这将引用YourProject.Common项目并从该项目返回DTO。 您无需在此项目中保留连接字符串以供您的应用程序运行,因为这将始终从启动时调用具有连接字符串的项目。但是,请保持App.Config (由EF生成),因为您需要在以后更新EDMX文件以进行数据库更改时。

  • Business ( BL layer, This project will have 2 project references, The Common Project and Data Access project) 业务 (BL层,这个项目将有2个项目参考,公共项目和数据访问项目)

  • Web/Win forms ( This is your front end/Startup project. This will not directly talk to the Data Access, This will have 2 project references , Common and Data Access). Web / Win表单 (这是您的前端/启动项目。这不会直接与数据访问对话,这将有2个项目引用,Common和Data Access)。 This will have your connection string. 这将有你的连接字符串。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM