Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 880
kiss my armpit Avatar asked Jan 16 '12 08:01

kiss my armpit


2 Answers

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.

like image 137
Juan Avatar answered Oct 21 '22 00:10

Juan


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.

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.

like image 33
Anders Abel Avatar answered Oct 20 '22 23:10

Anders Abel