Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Optimizing Entity Framework Performance

2.40/5 (4 votes)
23 Sep 2015CPOL2 min read 8K  
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)