---
title: "What is the purpose of the Include() method in EF?"  
description: "What is the purpose of the Include() method in EF?"  
author: "Ravi Vishwakarma"  
published: 2025-06-13  
updated: 2025-06-13  
canonical: https://www.mindstick.com/interview/34243/what-is-the-purpose-of-the-include-method-in-ef  
category: "c#"  
tags: ["c#", "entity framework"]  
reading_time: 4 minutes  

---

# What is the purpose of the Include() method in EF?

> The `Include()` method in Entity Framework is used for **eager loading** related entities — it tells EF to **load associated data** along with the main entity in a single query.

## Why Use `Include()`?

By default, Entity Framework uses **lazy loading** (if enabled), which means related entities are loaded **only when accessed** — often resulting in **N+1 queries**.

Using `Include()`:

- Reduces round-trips to the database
- Improves performance for related data
- Loads navigation properties *upfront* (eagerly)

## Example Scenario

Imagine you have two entities:

```cs
public class Order
{
    public int Id { get; set; }
    public Customer Customer { get; set; }
    public ICollection<OrderItem> Items { get; set; }
}

public class OrderItem
{
    public int Id { get; set; }
    public string Product { get; set; }
}
```

### Without `Include()`:

```cs
var orders = context.Orders.ToList();
// accessing order.Items will cause separate queries (lazy loading)
```

### With `Include()`:

```cs
var orders = context.Orders
    .Include(o => o.Items)
    .ToList(); // Loads Orders + related Items in 1 query
```

## Multiple Levels of Include (Nested Navigation)

```cs
var orders = context.Orders
    .Include(o => o.Customer)
    .Include(o => o.Items)
    .ToList();
```

### Deep Include (EF Core Only)

```cs
.Include(o => o.Items.Select(i => i.Product))
```

> In EF6, for multi-level include, you use `ThenInclude()` in EF Core or string-based paths in EF6:

```cs
.Include("Items.Product") // EF6
```

## Notes

| Behavior | Details |
| --- | --- |
| **Eager Loading** | Loads related data immediately via `Include()` |
| **Lazy Loading** | Loads related data on first access (must be enabled) |
| **Explicit Loading** | You load related data manually using `Load()` |

## Summary

| Feature | `Include()` Method |
| --- | --- |
| Purpose | Eagerly load related entities |
| Helps avoid | Lazy loading performance issues |
| Common use cases | Parent-child relationships (e.g., Orders + Items) |
| EF versions | Available in EF6 and EF Core |

## Answers

### Answer by Ravi Vishwakarma

> The `Include()` method in Entity Framework is used for **eager loading** related entities — it tells EF to **load associated data** along with the main entity in a single query.

## Why Use `Include()`?

By default, Entity Framework uses **lazy loading** (if enabled), which means related entities are loaded **only when accessed** — often resulting in **N+1 queries**.

Using `Include()`:

- Reduces round-trips to the database
- Improves performance for related data
- Loads navigation properties *upfront* (eagerly)

## Example Scenario

Imagine you have two entities:

```cs
public class Order
{
    public int Id { get; set; }
    public Customer Customer { get; set; }
    public ICollection<OrderItem> Items { get; set; }
}

public class OrderItem
{
    public int Id { get; set; }
    public string Product { get; set; }
}
```

### Without `Include()`:

```cs
var orders = context.Orders.ToList();
// accessing order.Items will cause separate queries (lazy loading)
```

### With `Include()`:

```cs
var orders = context.Orders
    .Include(o => o.Items)
    .ToList(); // Loads Orders + related Items in 1 query
```

## Multiple Levels of Include (Nested Navigation)

```cs
var orders = context.Orders
    .Include(o => o.Customer)
    .Include(o => o.Items)
    .ToList();
```

### Deep Include (EF Core Only)

```cs
.Include(o => o.Items.Select(i => i.Product))
```

> In EF6, for multi-level include, you use `ThenInclude()` in EF Core or string-based paths in EF6:

```cs
.Include("Items.Product") // EF6
```

## Notes

| Behavior | Details |
| --- | --- |
| **Eager Loading** | Loads related data immediately via `Include()` |
| **Lazy Loading** | Loads related data on first access (must be enabled) |
| **Explicit Loading** | You load related data manually using `Load()` |

## Summary

| Feature | `Include()` Method |
| --- | --- |
| Purpose | Eagerly load related entities |
| Helps avoid | Lazy loading performance issues |
| Common use cases | Parent-child relationships (e.g., Orders + Items) |
| EF versions | Available in EF6 and EF Core |


---

Original Source: https://www.mindstick.com/interview/34243/what-is-the-purpose-of-the-include-method-in-ef

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
