Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C#
Tip/Trick

Optimizing Entity Framework Performance

Rate me:
Please Sign up or sign in to vote.
2.40/5 (4 votes)
23 Sep 2015CPOL2 min read 7.7K   1   1
This article decries optimizing entity framework performance when using Code First approach with lazy loading enabled

Introduction

It is aware that child objects (Navigation Properties) will be loaded when it is first time accessed with the lazy loading techniques. This automatic loading increases the query execution time and decrease the performance. Hence, the first idea is to defer the lazy loading by manually querying the object with object type and id(s).  The second idea is to get the child object only on demand and don’t get entire object at first time.It is aware that child objects (Navigation Properties) will be loaded when it is first time accessed with the lazy loading techniques. This automatic loading increases the query execution time and decrease the performance. Hence, the first idea is to defer the lazy loading by manually querying the object with object type and id(s).  The second idea is to get the child object only on demand and don’t get entire object at first time.

Background

Fare knowledge in following Entity Framework concepts

  • Code First Approach
  • Lazy loading
  • DbContext
  • Modeling and Object Oriented Programming Concepts
  • MVVM – Model-View-View Model

Using the code

1. Create ID property for each and every navigation property

This id is required to get the navigation property from the entity framework during querying the object. The name of this id property should contain the associated navigation property name followed by Id. For example, if the Navigation property name is “Department” in the “Employee” class then the id property name should be named as “DepartmentId’

Note: if the above naming convention is not followed, it is mandatory to put the ForeignKeyAttribute to the Navigation property by specifying its associated id property name explicitly.

The data type of the id property depends on the data type of the associated navigation property object.

The id property should be nullable if the navigation property is optional.

C#
public class Employee
{
    public int id { get; set; }
    public string Name { get; set; }
    public Department Department { get; set; }
    public int? DepartmentId { get; set; }
}
public class Department
{
    public Department()
    {

    }
    public int Id { get; set; }
    public string Name { get; set; }
}

 

2.     Get the child object with type and id and assign it to the navigation property

C#
var ctx = new EmployeeContext("connection string");
           var empl = ctx.Employees.First();
           empl.Department =  ctx.Set(typeof(Department)).Find(empl.DepartmentId) as Department;

Points of Interest

 

History

Keep a running update of any changes or improvements you've made here.

License

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


Written By
Technical Lead IGATE
India India
I am currently working as technical lead in IGATE and have total of 8 years of experience in developing .Net technologies such as WPF with PRISM & MEF, C#, Entity Framework and solid background in software development life cycle process such as Agile, Six Sigma and Iterative Process.

Comments and Discussions

 
GeneralMy vote of 1 Pin
cjb11023-Sep-15 21:23
cjb11023-Sep-15 21:23 

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.