0

I started with database first approach with a many to one relation between Employee and Department. Two partial classes were created by Entity framework: Department having collections of Employee and Employee having single object Department.

If I added virtual then Department loads the related employees. There is no Inhertence relation ship between two classes. both are TPT.

I got this link saying

Lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.

So how is this happening? Department is not the Parent for Employee.

5
  • 1
    Please check this also stackoverflow.com/questions/11469432/… Commented Apr 16, 2015 at 4:44
  • 2
    It sounds like adding virtual resulted in lazy loading exactly like the MSDN link said it would. What is the problem here? Commented Apr 16, 2015 at 4:50
  • In my solution Employee and Department both are partial classes and showing Composition not Inheritance relationship. So Does EF behind the scene created proxy of both of the class and Implement Inheritence so that I can access Department d= new Employee() and with d I am loading all the Employee Object. Commented Apr 16, 2015 at 5:05
  • @user3007228 No That's wrong. proxy classes inherit from your classes Department and Employee. Commented Apr 16, 2015 at 5:14
  • When creating instances of POCO entity types, the Entity Framework often creates instances of a dynamically generated derived type that acts as a proxy for the entity. This proxy overrides some virtual properties of the entity to insert hooks for performing actions automatically when the property is accessed. For example, this mechanism is used to support lazy loading of relationships. The techniques shown in this topic apply equally to models created with Code First and the EF Designer. Commented Apr 16, 2015 at 11:28

2 Answers 2

2

Entity framework navigation properties work differently depending on whether you use a database-first or code-first approach. Here's an expanded snippet from the link you posted:

When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.

"POCO" means "plain old CLR object," which are the classes you would create in a code-first approach. Since those classes don't have any inherent knowledge of EF, you have to define your properties in such a way that the EF proxies can connect them correctly.

Since you are using database-first, the classes are not "POCO." They inherit from an entity framework base class that wires up the navigation properties for lazy loading.

Sign up to request clarification or add additional context in comments.

Comments

1

It seems you are confused about how proxy can do this.

So when you get the employee.Department property loaded with instance of Department, the instance employee is not of type Employee--instead it is of the type proxy class generated by EF and inherited from your Employee class. The allows the proxy type to override the Department property from the Employee class and that property's get method fires the database query to load the department instance into memory.

However you can also disable that behavior of proxy creation.

DbContext.Configuration.ProxyCreationEnabled = false;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.