为什么我们需要添加一个引用到装配,从一个类库项目继承,成为消费项目?项目、类库

2023-09-03 01:33:54 作者:别再把我弄丢了

我的解决方案包括两个项目:

My solution consists of two projects:

在一个类库项目(命名为数据访问),它定义一个类(命名为 MyContext )继承的DbContext 。由于的DbContext EntityFramework.dll 定义组件,我需要一个引用到本次大会到这个类库项目。我没有混淆了这种机制。 在一个控制台应用程序项目(命名为测试)消耗的类库上面定义的数据访问类。添加引用类库上面的组件是可以理解的。不幸的是,我不能编译的解决方案,直到我添加的引用 EntityFramework.dll 到这个控制台应用程序。 a class library project (named as DataAccess) that defines a class (named as MyContext) inheriting from DbContext. As DbContext is defined in EntityFramework.dll assembly, I need to add a reference to this assembly into this class library project. I have no confusion in this mechanism. a console application project (named as Tester) that consumes the DataAccess class defined in the class library above. Adding a reference to the assembly of the class library above is understandable. Unfortunately, I cannot compile the solution until I add a reference to EntityFramework.dll into this console application.

为什么我们需要添加一个引用到装配,从一个类库项目继承,成为消费项目? 在我的心智模式,它足以只添加的引用数据访问项目组装成测试项目和链接引用应自动执行。

Why do we need to add a reference to an assembly, from which a class library project inherits, into a consumer project? In my mental model, it is enough to only add a reference to DataAccess project assembly into Tester project and the chained references should be done automatically.

推荐答案

由于大量的 MyContext 的行为是从实体框架中的基类继承。如果你调用这些方法(例如:的SaveChanges())从测试测试应用程序需要引用其中的方法定义的类库。

Because a lot of the behaviour in MyContext is inherited from the base class in entity framework. If you call those methods (e.g. SaveChanges()) from Tester, the Tester app needs a reference to the class library where the method is defined.

如果您 MyContext 那是在你的类库中只定义了访问方式,我觉得你可以没有提及的基类。

If you would only access methods of MyContext that are defined in your class library, I think you could do without the reference to the base class.