Why do we need to add a reference to the assembly from which the class library project inherits into the consumer project?

My solution consists of two projects:

  • a class library project (called DataAccess ) that defines a class (called MyContext ) that inherits from DbContext . Since DbContext defined in the EntityFramework.dll assembly, I need to add a link to this assembly in this class library project. I have no confusion about this mechanism.
  • A console application project (called Tester ) that uses the DataAccess class defined in the above class library. Adding a reference to the assembly of the class library above is understandable. Unfortunately, I cannot compile the solution until I add a link to EntityFramework.dll in this console application.

Why do we need to add a reference to the assembly from which the class library project inherits into the consumer project? In my mental model, it’s enough to add a link to the DataAccess assembly of the project in the Tester project, and the constrained links should be executed automatically.

+4
source share
2 answers

Because most of the behavior in MyContext inherited from the base class in the entity structure. If you call these methods (e.g. SaveChanges() ) from Tester , the Tester application requires a reference to the class library where the method is defined.

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

+3
source

Because otherwise EntityFramework.dll will not be copied to the output directory when creating the project, and your library will not be able to access it.

If you can insert EntityFramework.dll into your library (right-click the link β†’ Properties), you will not need to reference it in the console application.

+3
source

Source: https://habr.com/ru/post/1391216/


All Articles