Skip to main content

Lesson 7 of 8

Data Mapping Strategies - Projection vs AutoMapper

For reads, direct EF Core projection (.Select()) is more performant than loading entities then AutoMapper-mapping - the projection generates a SQL SELECT with only the needed columns. AutoMapper is better for write paths where you map request → entity.

15 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Projection vs AutoMapper - When to Use Each

Projection vs AutoMapper - When to Use Each
// ━━ BETTER for reads: EF Core projection - SQL only fetches needed columns ━━
var products = await dbContext.Products
    .Where(p => p.IsActive && !p.IsDeleted)
    .Select(p => new ProductListDto(                // SQL: SELECT Id, Name, Price, c.Name
        p.Id, p.Name, p.Price,
        p.Category.Name,
        p.StockQuantity > 0))
    .AsNoTracking()
    .ToListAsync(ct);
// Generated SQL: SELECT p.Id, p.Name, p.Price, c.Name FROM Products p JOIN Categories c ON ...

// ━━ AutoMapper - good for write paths (request → entity) ━━
// Map incoming request to domain entity for persist:
var entity = mapper.Map<Product>(createRequest);
dbContext.Products.Add(entity);
await dbContext.SaveChangesAsync(ct);

// ━━ ProjectTo - AutoMapper + EF Core projection (best of both worlds) ━━
// dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection
var products2 = await dbContext.Products
    .Where(p => p.IsActive)
    .ProjectTo<ProductListDto>(mapper.ConfigurationProvider) // SQL projection via AutoMapper
    .ToListAsync(ct);

// ━━ Mapster - modern alternative to AutoMapper ━━
// dotnet add package Mapster
// Compile-time mapping with source generators - faster than AutoMapper reflection
// TypeAdapterConfig<Product, ProductListDto>.NewConfig()
//     .Map(d => d.CategoryName, s => s.Category.Name);
// var dto = product.Adapt<ProductListDto>();

// ━━ Rule of thumb ━━
// Reads (API list/detail endpoints) → EF Core .Select() or .ProjectTo<>()
// Writes (create/update)            → mapper.Map<Entity>(request)
// Small objects (<5 properties)     → manual mapping (fastest, most readable)

record ProductListDto(Guid Id, string Name, decimal Price, string CategoryName, bool IsAvailable);
interface IMapper { TDest Map<TDest>(object src); Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigurationProvider { get; } }

Tip

Tip

Practice Data Mapping Strategies Projection vs AutoMapper in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

.NET provides a unified platform for building various application types

Practice Task

Note

Practice Task - (1) Write a working example of Data Mapping Strategies Projection vs AutoMapper from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Data Mapping Strategies Projection vs AutoMapper is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready dotnet code.

Topics in This Module

0/8 done