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.

DWright
  • 9,258
  • 4
  • 36
  • 53
AmitykSharma
  • 127
  • 11
  • 1
    Please check this also http://stackoverflow.com/questions/11469432/entity-framework-code-first-lazy-loading – AmitykSharma Apr 16 '15 at 04:44
  • 2
    It sounds like adding `virtual` resulted in lazy loading exactly like the MSDN link said it would. What is the problem here? – BJ Myers Apr 16 '15 at 04: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. – AmitykSharma Apr 16 '15 at 05:05
  • @user3007228 No That's wrong. proxy classes inherit from your classes `Department` and `Employee`. – Jenish Rabadiya Apr 16 '15 at 05: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. – AmitykSharma Apr 16 '15 at 11:28

2 Answers2

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.

BJ Myers
  • 6,617
  • 6
  • 34
  • 50
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;
DWright
  • 9,258
  • 4
  • 36
  • 53
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62