简体   繁体   English

如何在项目中“携带”引用

[英]How to “carry” references across projects

I have 2 projects: X and Y. Project X is a class lib that references, lets say, EF 6. Project Y as a console app that references X. Why do i get a cannot load dependency exception if i do not add, lets say, EF 6 to project Y? 我有2个项目:X和Y.Project X是一个类lib,它引用,例如,EF 6.项目Y作为引用X的控制台应用程序。为什么我得到一个无法加载依赖项异常,如果我不添加,让比方说,EF 6投射Y? Is there a way to "carry" those references to project Y? 有没有办法“携带”那些对项目Y的引用?

The idea here is that Y should not know about EF-6, not to say its dependencies! 这里的想法是Y不应该知道EF-6,更不要说它的依赖!

You are very probably exposing classes to Y that inherit from Entity Framework, most probably your Entities, your Context if project Y uses that, etc. 您很可能将类暴露给Y,这些类继承自Entity Framework,很可能是您的实体,如果项目Y使用了您的Context,等等。

Therefore, project Y needs to reference Entity Framework, because it cannot otherwise resolve the classes (base properties, logic) that your classes inherit from. 因此,项目Y需要引用实体框架,因为它无法以其他方式解析类继承的类(基本属性,逻辑)。

The only way to hide Entity Framework from Y is to make it completely independent of it. 从Y隐藏实体框架的唯一方法是使其完全独立于它。 So for instance you may create a service class in project X that project Y calls, and this class' methods does not expose any classes from Entity Framework, nor any classes that inherit from classes in Entity Framework. 因此,例如,您可以在项目X中创建一个项目Y调用的服务类,并且此类的方法不公开来自Entity Framework的任何类,也不公开任何从Entity Framework中的类继承的类。 You can do that by creating your own entities that you then map to from queries in Entity Framework etc. 您可以通过创建自己的实体来实现,然后从实体框架等中的查询映射到这些实体。

Another way is to hide project X behind a webservice (WCF). 另一种方法是将项目X隐藏在Web服务(WCF)之后。 Then project Y calls the webservice, which calls the logic in project X. The WCF webservice exposes nothing from Entity Framework, so your project Y does not need any references to it. 然后项目Y调用webservice,它调用项目X中的逻辑.WCF Web服务不会从Entity Framework中公开任何内容,因此您的项目Y不需要任何对它的引用。

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

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