Entity Framework Core Interview Question #43
What is projection in EF Core?
Relationships, Navigation Properties & Loading Data Junior Beginner
Quick Interview Answer
Projection uses Select to retrieve only the columns and related values needed by the application instead of materializing complete entity graphs.
Detailed Explanation
Projection uses Select to retrieve only the columns and related values needed by the application instead of materializing complete entity graphs.
It is often ideal for read-only pages and APIs because it reduces transferred data, avoids unnecessary tracking, and can generate efficient SQL.
Projection also separates the database entity model from the response or presentation model.
Code Example
var products = await db.Products
.AsNoTracking()
.Select(p => new ProductListDto
{
Id = p.Id,
Name = p.Name,
CategoryName = p.Category.Name
})
.ToListAsync();