ASP.NET/ASP.NET Core

[ASP.Net Core] Entity Framework Core 2 - LINQ ( Include , ThenInclude )

http://portfolio.wonpaper.net 2023. 9. 30. 11:20

SQL 쿼리문에서 Join 문과 같이 관계가 연결되있는 부분을 LINQ 의 Include 와 ThenInclude 를 통하여 불러올수 있는데, 이에  좋은 포스팅 글이 있어서 소개한다.

 

https://entityframeworkcore.com/querying-data-include-theninclude

 

EF Core Include - Learn How to Retrieve Related Objects in LINQ

Include The Include method specifies the related objects to include in the query results. It can be used to retrieve some information from the database and also want to include related entities. Now let's say we have a simple model which contains three ent

entityframeworkcore.com

 

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
28
29
30
31
32
33
34
35
public class Customer
{
    public int CustomerId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public virtual List<Invoice> Invoices { get; set; }
}
 
public class Invoice
{
    public int InvoiceId { get; set; }
    public DateTime Date { get; set; }
    public int CustomerId { get; set; }
    [ForeignKey("CustomerId")]
    public Customer Customer { get; set; }
    public List<InvoiceItem> Items { get; set; }
}
 
public class InvoiceItem
{
    public int InvoiceItemId { get; set; }
    public int InvoiceId { get; set; }
    public string Code { get; set; }
    [ForeignKey("InvoiceId")]
    public virtual Invoice Invoice { get; set; }
}
 
Controller 에서
 
var customer = db.Customers.Single(c => c.CustomerId == cid);
 
var invoices = db.Customers.Include(c => c.Invoices).ThenInclude(c2 => c2.InvoiceItems)
                .Where(c3 => c3.CustomerId == cid).ToList();
 
cs

 

 

https://learn.microsoft.com/ko-kr/ef/core/querying/related-data/eager

 

관련 데이터의 즉시 로드 - EF Core

Entity Framework Core에서 관련 데이터의 즉시 로드

learn.microsoft.com

https://learn.microsoft.com/ko-kr/ef/core/modeling/entity-types?tabs=data-annotations 

 

엔터티 형식 - EF Core

Entity Framework Core를 사용하여 엔터티 형식을 구성하고 매핑하는 방법

learn.microsoft.com

 

[ MS 의 아주 좋은 EF 예제 ] - 다대다 관계 등 다양한 내용 수록

 

https://learn.microsoft.com/ko-kr/aspnet/core/data/ef-mvc/complex-data-model?view=aspnetcore-6.0 

 

자습서: 복잡한 데이터 모델 만들기 - ASP.NET MVC 및 EF Core 사용

이 자습서에서는 더 많은 엔터티 및 관계를 추가하고, 서식 지정, 유효성 검사 및 매핑 규칙을 지정하여 데이터 모델을 사용자 지정합니다.

learn.microsoft.com

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Include(blog => blog.Posts)
        .Include(blog => blog.Owner)
        .ToList();
}
 
 
using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Include(blog => blog.Posts)
        .ThenInclude(post => post.Author)
        .ThenInclude(author => author.Photo)
        .Include(blog => blog.Owner)
        .ThenInclude(owner => owner.Photo)
        .ToList();
}
cs

 

https://www.tutorialsteacher.com/linq/what-is-linq

 

What is LINQ

What is LINQ? Language-Integrated Query (LINQ) is a powerful set of technologies based on the integration of query capabilities directly into the C# language. LINQ Queries are the first-class language construct in C# .NET, just like classes, methods, event

www.tutorialsteacher.com

 

https://www.tutorialsteacher.com/linq/linq-method-syntax

 

LINQ Method Syntax

LINQ Method Syntax In the previous section, you have learned about LINQ Query Syntax. Here, you will learn about Method syntax. Method syntax (also known as fluent syntax) uses extension methods included in the Enumerable or Queryable static class, similar

www.tutorialsteacher.com

 

https://www.tutorialsteacher.com/linq/linq-lambda-expression

 

Anatomy of the Lambda Expression

Anatomy of the Lambda Expression C# 3.0(.NET 3.5) introduced the lambda expression along with LINQ. The lambda expression is a shorter way of representing anonymous method using some special syntax. For example, following anonymous method checks if student

www.tutorialsteacher.com

 

 

 

[ DTO 관련 내용 설명 ]  

https://www.telerik.com/blogs/dotnet-basics-dto-data-transfer-object

 

.NET Basics: DTO (Data Transfer Object)

Something common in ASP.NET Core development is data transfer. A good practice is to use DTOs that will define how the data will be sent over the network.

www.telerik.com

 

 

[ EF Core Performance Optimization Challenge ]

https://www.youtube.com/watch?v=jSiGyPHqnpY 

https://github.com/StefanTheCode/OptimizeMePlease

 

GitHub - StefanTheCode/OptimizeMePlease

Contribute to StefanTheCode/OptimizeMePlease development by creating an account on GitHub.

github.com

 

 

 

 

 

참고 : https://dleunji.tistory.com/45?category=999005

참고 : https://learn.microsoft.com/ko-kr/aspnet/core/data/ef-mvc/read-related-data?view=aspnetcore-6.0

참고 : https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/data/ef-mvc/intro/samples/cu-final

 

ASP.NetCore 깃허브 자료실

https://github.com/dotnet/AspNetCore.Docs

 

GitHub - dotnet/AspNetCore.Docs: Documentation for ASP.NET Core

Documentation for ASP.NET Core. Contribute to dotnet/AspNetCore.Docs development by creating an account on GitHub.

github.com