관리 메뉴

웹개발자의 기지개

[ASP.Net Core] EF 에서 Include 와 ThenInclude 사용하기 본문

ASP.NET/ASP.NET Core

[ASP.Net Core] EF 에서 Include 와 ThenInclude 사용하기

웹개발자 워니 2026. 7. 1. 15:09

ORM Entity Framework 에서 Include / ThenInclude 사용법을 몇가지 정리해 본다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
 
    public List<ProductOption> Options { get; set; }
}
 
public class ProductOption
{
    public int Id { get; set; }
    public string OptionName { get; set; }
 
    public int ProductId { get; set; }
    public Product Product { get; set; }
 
    public List<ProductOptionValue> Values { get; set; }
}
 
public class ProductOptionValue
{
    public int Id { get; set; }
    public string ValueName { get; set; }
 
    public int ProductOptionId { get; set; }
    public ProductOption ProductOption { get; set; }
}
cs

 

 

상품
 └─ 옵션들
     └─ 옵션값들

 

Product
 └── ProductOption
       └── ProductOptionValue

 

티셔츠
 ├─ 색상
 │   ├─ 빨강
 │   ├─ 파랑
 │   └─ 검정
 └─ 사이즈
     ├─ S
     ├─ M
     └─ L

 

 

var products = db.Products
    .Include(p => p.Options)
    .ToList();

 

Product
 └── Options

 

var products = db.Products
    .Include(p => p.Options)
        .ThenInclude(o => o.Values)
    .ToList();

Product
 └── Options
       └── Values

 

 

var product = db.Products
    .Include(p => p.Options)
    .FirstOrDefault(p => p.Id == 1);

 

 

가져오는 데이터:
상품명: 티셔츠
옵션:
- 색상
- 사이즈

하지만 옵션값은 없습니다.
색상 안의 빨강, 파랑, 검정
사이즈 안의 S, M, L

 

 

var product = db.Products
    .Include(p => p.Options)
        .ThenInclude(o => o.Values)
    .FirstOrDefault(p => p.Id == 1);


상품명: 티셔츠
옵션:
- 색상
  - 빨강
  - 파랑
  - 검정

- 사이즈
  - S
  - M
  - L

 

예제1)

var product = db.Products
    .Include(p => p.Options)
        .ThenInclude(o => o.Values)
    .FirstOrDefault(p => p.Id == id);


Razor View에서:
<h2>@Model.Name</h2>
@foreach (var option in Model.Options)
{
    <h4>@option.OptionName</h4>
    <ul>
    @foreach (var value in option.Values)
    {
        <li>@value.ValueName</li>
    }
    </ul>
}

출력:
티셔츠
색상
- 빨강
- 파랑
- 검정
사이즈
- S
- M
- L

-----------------

 

1
2
3
4
5
6
7
8
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
 
    public List<ProductOption> Options { get; set; }
    public List<ProductImage> Images { get; set; }
}
cs

 

var products = db.Products
    .Include(p => p.Options)
    .Include(p => p.Images)
    .ToList();

Product
 ├── Options
 └── Images

 

여러 단계로 내려가 있는 경우 (주문하기)

Order
 └── OrderItems
       └── Product
             └── Category

 

var orders = db.Orders
    .Include(o => o.OrderItems)
        .ThenInclude(oi => oi.Product)
            .ThenInclude(p => p.Category)
    .ToList();

주문을 가져오고
 └─ 주문상세를 가져오고
     └─ 상품을 가져오고
         └─ 상품 카테고리까지 가져온다

 

 

Include         현재 엔티티의 직접 연결 데이터를 가져옴
ThenInclude Include로 가져온 데이터의 하위 데이터를 가져옴

------------------

 

var product = db.Products
    .Include(p => p.Options)
        .ThenInclude(o => o.Values)
    .Include(p => p.Images)
    .Include(p => p.Category)
    .FirstOrDefault(p => p.Id == id);

상품 1개를 가져오면서
- 옵션
  - 옵션값
- 이미지
- 카테고리
를 함께 가져온다

 

--------------

 

Include를 너무 많이 쓰면 SQL JOIN이 많아져서 성능이 떨어질 수 있습니다.

db.Products
    .Include(p => p.Options)
        .ThenInclude(o => o.Values)
    .Include(p => p.Images)
    .Include(p => p.Reviews)
    .Include(p => p.Orders)
    .ToList();

이 코드는 다음과 같이 일부 코드로 줄여서,
성능을 개선할 수 있습니다.

var products = db.Products
    .Select(p => new
    {
        p.Id,
        p.Name,
        Options = p.Options.Select(o => new
        {
            o.OptionName,
            Values = o.Values.Select(v => v.ValueName)
        })
    })
    .ToList();

 

 

Include vs ThenInclude.pdf
0.65MB

 

Comments