简体   繁体   English

为什么已经从另一个项目添加了此引用,为什么还要添加相同的引用

[英]Why I need to add same reference when this reference had been added from another project

Here is a Question, Supposed that I have two projects: 这是一个问题,假设我有两个项目:

  1. Application(console) 应用程序(控制台)
  2. DataAccess(class library). DataAccess(类库)。

In DataAccess project, there is a DataContext class inherited from DbContext 在DataAccess项目中,有一个从DbContext继承的DataContext类。

public class DataContext : DbContext {...}

In Application project, I has added DataAccess reference, and in main method I added following statement: 在Application项目中,我添加了DataAccess参考,在main方法中,添加了以下语句:

using (var context = new DataContext ()){...}

but Resharper show 但是Resharper秀

The type DbContext is defined in an assembly not referenced. 类型DbContext是在未引用的程序DbContext定义的。

Why do I need to add the same assembly again? 为什么需要再次添加相同的程序集?

Why can't I get DbContext definition from DataAccess.dll? 为什么无法从DataAccess.dll获取DbContext定义?

(DataAccess project has added EntityFramwork.dll through NuGet) (DataAccess项目已通过NuGet添加了EntityFramwork.dll)

Edit 1: When a type inherited another type(ex: DataContext inherited from DbContext), why compiler didn't include the definition of DbContext in the same dll, so in another project, we just only need to add reference to child type's dll ? 编辑1:当一个类型继承另一个类型(例如:DataContext从DbContext继承)时,为什么编译器没有在同一个dll中包含DbContext的定义,所以在另一个项目中,我们只需要添加对子类型的dll的引用?

Because your class library (the .dll ) doesn't 'contain' its dependent assemblies. 因为您的类库( .dll )不“包含”其依赖程序集。 So your executing assembly needs to have references to the dependent assemblies too if it wants to use your class library. 因此,如果要执行的程序集要使用您的类库,则也需要引用依赖程序集。

(It's a bit the same as with config files, they aren't embedded either, so even if you have for example a connectionstring defined in the config file of the class library, you still need to define it into your start-up application too.) (与配置文件有点相同,它们也没有被嵌入,因此即使您在类库的配置文件中定义了一个连接字符串,也仍然需要将其定义到启动应用程序中。)

If you really want to embed libraries into a single dll you can take a look at ILMerge but it's recommended to just include all the libraries you need in your start-up application. 如果您确实希望将库嵌入单个dll中,则可以查看ILMerge,但建议仅在启动应用程序中包含所需的所有库。

Another good one (that I can recommend) is Costura.Fody . 另一个不错的(我推荐)是Costura.Fody You just need to install it through NuGet, no configuration required whatsoever and when you build your application the next time, it will embed everything into your class library. 您只需要通过NuGet进行安装,就不需要任何配置,下次您构建应用程序时,它将把所有内容都嵌入到类库中。

Because the type you want to reach is on that assembly. 因为您要到达的类型在该装配件上。 Because you want to be able to define a variable on that type. 因为您希望能够在该类型上定义变量。 It is like: 它像是:

DataContext dbContext = new DataContext();
dbContext.Users.ToList();

this is what you want to do in presentation layer using by DAL. 这是您要在DAL使用的表示层中执行的操作。 But the problem is you still using entity framework. 但是问题是您仍在使用实体框架。 Lets create a function on service layer which has dependency on EntityFramework. 让我们在服务层上创建一个依赖EntityFramework的函数。

List<User> GetUsers()
{
DataContext dbContext = new DataContext();
dbContext.Users.ToList();
}

And now we can simply call this service and function without adding Entity Framework reference. 现在我们可以简单地调用此服务和函数,而无需添加实体框架引用。 That's because; 那是因为; In presentation layer, we have nothing to do with entity framework right now. 在表示层中,我们现在与实体框架无关。 We are just calling a function. 我们只是在调用一个函数。 But in your case you are just using entity framework with the Context from another project. 但是在您的情况下,您只是将实体框架与另一个项目中的Context一起使用。 That's why your code needs EF refence and mine doesnt. 这就是为什么您的代码需要EF引用,而我的则不需要。 Thanx 感谢名单

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

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