Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#

Typed Eager Loading Using Entity Framework (& What is Eager Loading vs Deferred Loading)

Rate me:
Please Sign up or sign in to vote.
4.83/5 (4 votes)
7 Apr 2009CPOL5 min read 34.4K   7   6
Typed Eager Loading using Entity Framework (& what is eager loading vs deferred loading)

If you don’t know what eager loading is, jump to “What’s eager loading?”.

Eager Loading Syntax

If you are eager loading Products for example in a typical (Categories 1<->* Products) relation, the standard syntax would look like:

C#
DbDataContext.Categories.Include("Products")

What is the Problem With That?

The “Products” part. The word “Products” is a string. If I rename the Products table to ShopProducts or whatever or even remove it from this data diagram and have it elsewhere, or even something wrong happens and the relation is removed from DB/diagram by mistake, my code will still compile, but will fail when it runs. This is BAD BAD BAD.

How to Solve This?

Since I always believe that if something exists somewhere, you shouldn’t do it yourself unless it's totally broken (and I mean REALLY REALLY BROKEN), I started searching inside the Entity Framework itself for something to get the entity name from.

At first, it seemed super easy. Every entity class has a static property “EntityKeyPropertyName”, so, I thought I can write something like:

C#
DbDataContext.Categories.Include(Product.EntityKeyPropertyName); // But this didn’t work

Where Product is the entity class generated for table “Products”. Note that singularizing the name (Products becomes Product) does not happen automatically like in Linq-To Sql, you’ll have to change it manually, which is not required for the code here of course.

As you can see in the comment, this didn’t work. The value of property was always “-EntityKey-”, the default value of the abstract class “StructuralObject” which all entity classes inherit.

I kept searching all over until I found that the only place I can get the name from was an Attribute generated on the class somewhat like this:

C#
[global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(
    NamespaceName="DatabaseNameFlowModel", Name="Products")]

My requirement was simple. If the diagram has something wrong that the relation between ParentTable and ChildTable tables, but not about the entity classes themselves, my code should not still compile and fail on run. I need to use some code that depends on the relation so that if something is wrong with the relation, this code fails early and I know about the problem the next time I build the VS project.

The Final Solution

I tried badly to get the entity name from the API, after getting frustrated, I ended up writing this code:

C#
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq.Expressions;
using System.Reflection;
namespace Meligy.Samples.EntityFramework
{
    public static class LinqExenstions    
    {
        //Used for child entities.        
        //Example: Order.Customer       
        public static ObjectQuery<T> Include<T>(this ObjectQuery<T> parent,
            Expression<Func<T, StructuralObject>> expression)
            where T : StructuralObject        {
                return Include(parent, (LambdaExpression) expression);
            }

            //Used for child collections of entities.        
            //Example: Order.OrderLines       
            public static ObjectQuery<T> Include<T>(this ObjectQuery<T> parent,  
                Expression<Func<T, RelatedEnd>> expression)
                where T : StructuralObject        {
                    return Include(parent, (LambdaExpression) expression);
                }
                private static ObjectQuery<T> Include<T>(ObjectQuery<T> parent,  
                    LambdaExpression expression)            
                    where T : StructuralObject    
                {

                    //There must be only one root entity to load related entities to it.  
                    if (expression.Parameters.Count != 1)         
                    {
                        throw new NotSupportedException();
                    }

                    //We'll store entity names here in order then join them at the end. 
                    var entityNames = new List<string>();

                    //We split the calls ... 
                    //Entity.MemberOfTypeChild.ChildMemberOfChildMember etc..            
                    //Example: (Order ord) => ord.Customer.Address          
                    string[] childTypesMembers = expression.Body.ToString().Split('.');

                    //Get the root entity type to start searching for the types of
                    //the members inside it.            
                    //In prev. example: Find: Order       
                    Type parentType = expression.Parameters[0].Type;

                    //entityNames.Add(GetEntityNameFromType(parentType));

                    //The first word in the expression is just a variable name of
                    //the root entity.             
                    //  Skip it and start next.            
                    //In example: First part is: ord      
                    for (int i = 1; i < childTypesMembers.Length; i++)        
                    {
                        string memberName = childTypesMembers[i];

                        //Get the member from the root entity to get its entity type.    
                        MemberInfo member = parentType.GetMember(memberName)[0];

                        //We cannot get the type of the entity except by knowing      
                        //  whether it's property or field (most likely will be property). 
                        //Bad catch in the reflection API? Maybe!       
                        Type memberType = member.MemberType == MemberTypes.Property     
                            ? ((PropertyInfo) member).PropertyType   
                            : ((FieldInfo) member).FieldType;

                        //Add the eneity name got from entity type to the list.         
                        entityNames.Add(GetEntityNameFromType(memberType));

                        //The next member is belong to the child entity, so,        
                        // the root entity to seach for members should
                        // be the child entity type.  
                        parentType = memberType;
                    }

                    //Join the entity names by "." again.     
                    string includes = string.Join(".", entityNames.ToArray());

                    //Simulate the original Include(string) call.         
                    return parent.Include(includes);
                }

                private static string GetEntityNameFromType(Type type)       
                {
                    // We didn't just use the Entity type names because maybe            
                    //  the table is called Orders and the class is Order or OrderEntity.  

                    if (type.HasElementType) //For arrays, like: OrderLines[]     
                    {
                        //The type of the element of the array is what we want.           
                        type = type.GetElementType();
                    }
                    // for collections, like: EntityCollection<OrderLines>   

                    else if (type.IsGenericType) 
                    {
                        var genericClassTypeParameters = type.GetGenericArguments();

                        //The generic class must have one entity type only to load it. 
                        if (genericClassTypeParameters.Length != 1)            
                            throw new NotSupportedException();

                        //The type of the element of the collection is what we want.      
                        type = genericClassTypeParameters[0];
                    }

                    //Get the attributes that have the entity name in them.  
                    var entityTypeAttributes =       
                        type.GetCustomAttributes(typeof (EdmEntityTypeAttribute),
                            true) as EdmEntityTypeAttribute[];

                    //Make sure there IS one and ONLY one attribute to get the
                    //only entity name.         
                    if (entityTypeAttributes == null || entityTypeAttributes.Length != 1)   
                        throw new NotSupportedException();

                    //Return the entity name.          
                    return entityTypeAttributes[0].Name;
                }
    }
}

This enables you to write:

C#
DbDataContext.Categories.Include( (cat)=> cat.Prodycts);

or:

C#
DbDataContext.Prodycts.Include( (prod)=> prod.Category);

According to your need.

For things like: Order.Customer.Address (multiple levels), you’ll have to write code like:

C#
DbDataContext.Orders.Include( order => order.Customer ).Include(
   customer => Customer.Address );

What’s Eager Loading? (in case you don’t know)

Let’s say you have tables Products and Categories with relation 1<->* between them (any category has many products; one product has one category). Let’s say you want to display a page of all products grouped by categories. Something like the following list but with much more information of course:

  • Category 1
    • Product A
    • Product B
    • Product C
  • Category 2
    • Product X
    • Product Y
    • Product Z

If you are using some ORM / Code generator that creates for you classes like “Product”, “Category” and gives you properties like “myCategory.Products” , “myProduct.Category”, how would you create such page?

Normally, you’ll put a repeater or such for products inside a repeater for categories.

image

The products repeater will have its data source set to the current category item of the Categories repeater, something like “( (CategoryEntity)Container.DataItem ).Products”. Fine with that? Familiar?

OK. Now, if the code generator that generated the “Products” property has something like that:

C#
public List<PRoduct> _Products; 
public List<PRoduct> Products 
{ 
    get 
    { 
        if (_Products == null) 
        { 
            _Products = (from products in DB.Products 
                         where products.CategoryID == this.ID 
                         select products) 
                .ToList(); 
        } 

        return _Products; 
    } 
}

* Nevermind the LINQ syntax. It’s just like writing “SELECT * FROM [Products] WHERE …” with all the dataset/datareader stuff.

Lazy Loading (AKA. Deferred Loading)

If the generated code (or your code) looks like this, this means that that for every category in the database, you’ll have a separate DB call to get the products of this category.

It also means that the products of each category will not be loaded until someone writes code that calls the getter of the Products property. That’s why this style of coding (not loading the related objects until they’re asked to be loaded) is called Lazy Loading.

This is good for a single category where you may be seeking just the basic information of the category and will not try to load products, since then they will not be requested when you don’t ask for it.

However, this is very bad for our example page. Because it means a new DB call for each category. Imagine that you have 20 or 50 or 100 Category there, this will give you how many DB calls? (Hint: N Categories + 1 call for the category list itself).

Eager Loading

What if the code in the getter above was in the constructor? Imagine something like:

C#
public Category(int categoryID) 
{ 
    // Code that laods category info from DB. Does not matter here. 
    //Could be in factory method or such. Not our topic 
    _CategoryID = categoryID; 
    // .... .... .... Continue Loading Logic

    //The important part 
    _Products = (from products in DB.Products 
                 where products.CategoryID == this.ID 
                 select products) 
                .ToList(); 
}

This is good in case you know that in every situation when you use the category, the Products will be needed. This is probably not useful in a Product/category scenario, but think of a Person table and Address table where most likely whenever you load a Person you’re going to load his Addresses.

This is also useful especially when using ORM/code generator as in the first example. Let's get back to the Repeater example. If you use Entity framework or similar ORM, and you set the Categories query to load the Products eager loading (meaning each Category is created with its Products loaded already), Entity Framework can have a single connection and only TWO database hits, one for the Categories, and one for the Products. This is very useful in many listing scenarios. It also helps especially when you have many parent objects (say Categories) or if the parent object needs to load entities of many different classes (say User needs to load Roles and Permissions and Personal Information and History and …. (if such case is applicable for you, of course.

Now that you know what eager loading is, you can go up and check how the Entity Framework does that.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Senior Consultant at Readify
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 203714016-May-11 20:46
Member 203714016-May-11 20:46 
GeneralAnother way Pin
Member 437597226-Jan-11 3:57
Member 437597226-Jan-11 3:57 
GeneralYou got my 5 Pin
Bishoy Demian13-May-09 6:06
Bishoy Demian13-May-09 6:06 
GeneralCongratulations Pin
Ayman M. El-Hattab12-May-09 22:05
Ayman M. El-Hattab12-May-09 22:05 
Hello Meligy,
That's Ayman El-Hattab, Congratulations man for the first article on codeproject Smile | :) Waiting for more ...

Ayman El-Hattab
http://ayman-elhattab.blogspot.com

Ayman M. El-Hattab
Microsoft Certified SharePoint Specialist
ITWorx Egypt
http://ayman-elhattab.blogspot.com

GeneralThanks Pin
Joao Prado12-May-09 19:30
Joao Prado12-May-09 19:30 
GeneralEF include issue Pin
Javier jimenez Rico9-Apr-09 7:09
professionalJavier jimenez Rico9-Apr-09 7:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.