为什么我们需要添加一个引用到装配,从一个类库项目继承,成为消费项目? [英] Why do we need to add a reference to an assembly, from which a class library project inherits, into a consumer project?
问题描述
我的解决方案包括两个项目:
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 asMyContext
) inheriting fromDbContext
. AsDbContext
is defined inEntityFramework.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 theDataAccess
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 toEntityFramework.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.
这篇关于为什么我们需要添加一个引用到装配,从一个类库项目继承,成为消费项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!