简体   繁体   中英

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:

  • 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.

Because otherwise EntityFramework.dll wont get copied to the output directory when you build your project and your library won't be able to access it.

If you are able to embed EntityFramework.dll into your library (right click the reference -> Properties) then you won't need to reference it on your console application.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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