← Back to Blog

ASP.NET Zero

AutoMapper to Mapperly Migration in ASP.NET Zero v15: A Step-by-Step Guide

AutoMapper to Mapperly Migration in ASP.NET Zero v15: A Step-by-Step Guide

Upgrading your ASP.NET Zero application can feel straightforward until you reach the object mapping layer.

Your application builds. Your entities are still there. Your DTOs look familiar. But the mapping configuration that has supported your application for years is changing.

Starting with ASP.NET Zero v15, the framework began moving away from AutoMapper and toward Mapperly. The migration was finalized in ASP.NET Zero v15.2, bringing Mapperly into the framework and its Power Tools.

For teams with older applications, this is more than replacing one NuGet package with another. You may have hundreds of mappings inside CustomDtoMapper.cs, DTOs decorated with [AutoMapFrom] and [AutoMapTo], custom conversion rules, and ObjectMapper.Map() calls throughout your AppServices.

The difficult part isn't creating new mapper classes. It's making sure the new mappings preserve the behavior your application already depends on.

This guide explains how to approach an AutoMapper to Mapperly migration in ASP.NET Zero, what to check before changing your code, and how to catch mapping problems that a successful build might not reveal. If you're planning the wider version jump too, start with our ASP.NET Zero 15.2 upgrade guide for fixing backend errors first.

Contents

Key Takeaways

  • ASP.NET Zero v15 introduced the transition to Mapperly, and v15.2 finalized the migration.
  • Mapperly generates mapping code at compile time instead of relying on runtime reflection.
  • Existing ObjectMapper.Map() calls can continue working with the ASP.NET Zero Mapperly integration.
  • Custom mapping rules, ignored properties, nested objects, and null handling deserve special attention.
  • Mapperly supports IQueryable projections, but those projections have specific limitations.
  • Test tenant-scoped DTOs before releasing the migration.
  • Avoid registering the AutoMapper and Mapperly ABP integrations together as competing IObjectMapper implementations.

Why Did ASP.NET Zero Move from AutoMapper to Mapperly?

AutoMapper has been a familiar part of many .NET applications. Developers use it to convert entities into DTOs, map incoming requests to existing objects, and manage complex mapping rules through centralized profiles.

However, the mapping ecosystem has changed.

AutoMapper's licensing model changed in newer releases, creating another consideration for commercial applications. At the same time, Mapperly offers a different approach: instead of configuring mappings that execute through runtime reflection, it generates C# mapping code during compilation.

ASP.NET Zero started this transition in v15.0 and finalized it in v15.2. The same release line also moved to .NET 10, which we cover in our guide to upgrading ASP.NET Zero to .NET 10.

The difference is important:

Area AutoMapper Mapperly
Mapping approach Runtime mapping configuration Compile-time source generation
Configuration style Centralized profiles Mapper classes
Mapping errors Often discovered at runtime Many issues reported during compilation
Generated code Runtime mapping behavior Generated C# code can be inspected
ASP.NET Zero integration Abp.AutoMapper Abp.Mapperly

This doesn't mean every existing application should immediately rewrite every mapping. The right migration approach depends on how much custom mapping logic your application contains.

If your solution has years of development behind it, start by understanding what you have before deciding what to replace. Our guide to upgrading ASP.NET Zero without breaking your custom code shows how to take that inventory across the whole solution.

How to Migrate from AutoMapper to Mapperly in ASP.NET Zero

A safe migration starts with an inventory, not a package update.

Here is the order to follow.

Step 1: Find Every AutoMapper Dependency

Before changing anything, search your complete solution for existing mapping configurations and usages.

Look for:

  • CreateMap< inside CustomDtoMapper.cs and other profile classes.
  • [AutoMapFrom], [AutoMapTo], and [AutoMap] on DTOs and entities.
  • ObjectMapper.Map<TDestination>() and ObjectMapper.Map<TSource, TDestination>() inside AppServices.
  • ProjectTo<TDestination>() in repository and database queries.
  • Custom ForMember, Ignore(), ConvertUsing, and AfterMap rules.
  • Custom ITypeConverter and IValueResolver implementations.

Don't limit the search to the Application project. Mapping logic can also exist in background jobs, event handlers, MVC projects, GraphQL, and other parts of a customized solution.

Create a mapping inventory with these columns:

Field What to record
Source type Entity or input DTO
Destination type Output DTO or entity
Project/module Where the mapping is used
Custom rules Ignore, conversion, or custom logic
Navigation properties Related entities accessed
Tenant scope Whether the data is tenant-specific
Test coverage Existing tests and missing scenarios

This list gives you two things: a migration checklist and a testing checklist. It also helps identify unused mappings that can be removed instead of converted.

Step 2: Separate Simple Mappings from Custom Logic

Not every AutoMapper configuration deserves the same migration effort.

Start with the mappings that have predictable behavior. Leave complex mappings until you understand their dependencies.

Mapping type Suggested approach Reason
Same-name properties Convert first Usually requires minimal configuration
Renamed properties Convert with attributes Explicit property mapping is available
Ignored properties Convert with ignore attributes The intention must remain clear
Nested objects Review and test carefully Null values and navigation paths can behave differently
ProjectTo queries Convert and inspect SQL Projection expressions have limitations
Heavy AfterMap logic Review separately Business logic may belong outside the mapper
Custom converters Convert individually Their behavior may not have a direct one-to-one replacement
Third-party generated code Assess before changing You may not own or control its mapping implementation

One important point: incremental migration does not mean registering both ABP mapper modules at the same time.

You can organize the conversion work into separate pull requests and functional areas. A clear upgrade branch strategy and conflict checklist helps keep that work manageable. However, the AutoMapper and Mapperly ABP integrations both provide IObjectMapper, so they should not be enabled together as competing implementations.

Plan the integration change carefully and follow the setup for your specific ASP.NET Zero version. If you need to retain AutoMapper for a legacy application, evaluate a supported or custom integration separately rather than assuming both modules can be registered together.

Step 3: Convert AutoMapper Profiles into Mapperly Classes

With AutoMapper, you may have managed most mappings from a centralized CustomDtoMapper.cs file.

For example:

configuration.CreateMap<Invoice, InvoiceDto>();

Mapperly uses partial mapper classes. The source generator creates the implementation during the build.

A basic mapper can look like this:

using Riok.Mapperly.Abstractions;

[Mapper]
public partial class InvoiceMapper
{
    public partial InvoiceDto Map(Invoice source);
}

For a larger application, you can organize mapper classes around related functionality and source/destination types.

For example:

[Mapper]
public partial class InvoiceToInvoiceDtoMapper
{
    public partial InvoiceDto Map(Invoice source);

    public partial List<InvoiceDto> Map(
        List<Invoice> source
    );
}

The ASP.NET Zero integration also allows existing application code to continue using the ObjectMapper API:

var invoiceDto = ObjectMapper.Map<InvoiceDto>(invoice);

That means the migration does not necessarily require rewriting every mapping call inside your AppServices.

However, the mapper classes and their registration must follow the conventions of the upgraded ASP.NET Zero template. Check the generated project and the migration documentation for the exact setup.

Step 4: Replace Custom Mapping Rules Carefully

This is where a simple conversion script can become risky.

AutoMapper profiles often contain rules that are easy to overlook. A property might be renamed, ignored, conditionally mapped, or populated through a custom converter.

Mapperly provides attributes and user-defined mapping methods for these scenarios.

Renamed Properties

AutoMapper:

configuration.CreateMap<User, UserDto>()
    .ForMember(
        dest => dest.Email,
        opt => opt.MapFrom(src => src.EmailAddress)
    );

Mapperly:

[Mapper]
public partial class UserMapper
{
    [MapProperty(
        nameof(User.EmailAddress),
        nameof(UserDto.Email)
    )]
    public partial UserDto Map(User user);
}

Ignored Properties

AutoMapper:

configuration.CreateMap<User, UserDto>()
    .ForMember(
        dest => dest.Password,
        opt => opt.Ignore()
    );

Mapperly:

[Mapper]
public partial class UserMapper
{
    [MapperIgnoreTarget(nameof(UserDto.Password))]
    public partial UserDto Map(User user);
}

Updating an Existing Entity

AutoMapper:

ObjectMapper.Map(input, existingInvoice);

Mapperly mapper definition:

[Mapper]
public partial class InvoiceMapper
{
    public partial void Map(
        EditInvoiceInput input,
        Invoice target
    );
}

The important part is to preserve the original behavior, not simply reproduce the old configuration syntax.

For example, if a property was intentionally excluded because it contains sensitive information, make sure it remains excluded after the conversion.

Step 5: Make Mapperly Warnings Part of Your Migration Checks

One advantage of Mapperly is that it reports mapping issues through compiler diagnostics.

Two useful examples are:

  • RMG012: A target member has no matching source member.
  • RMG020: A source member is not mapped to a target member.

These warnings can reveal properties that were renamed, removed, or unintentionally left out.

For converted projects, you can make them fail the build through .editorconfig:

[*.cs]
dotnet_diagnostic.RMG012.severity = error
dotnet_diagnostic.RMG020.severity = error

Don't automatically suppress every warning just to get a successful build.

For each warning, make a deliberate decision:

  1. Add the missing mapping.
  2. Ignore the property intentionally.
  3. Review whether the source or destination model has changed.
  4. Add a test if the property affects important application behavior.

A clean build is useful. A clean build with intentional mapping decisions is much more valuable.

Step 6: Test Null Values and Nested Navigation Properties

A mapper can compile successfully and still produce unexpected results when your application data is incomplete.

Consider a DTO that reads a customer's name through an invoice:

public class InvoiceDto
{
    public string? CustomerName { get; set; }
}

The mapping depends on this relationship:

invoice.Customer.Name

But what happens when Customer is null?

Or when the navigation property hasn't been loaded?

Or when the related record no longer exists?

These cases need explicit tests.

For every DTO that maps through a navigation property, test at least:

  • The related entity exists and is loaded.
  • The related entity is null.
  • The related entity has nullable fields.
  • The expected DTO value is returned in each case.

For example, don't assume that a missing customer should always produce an empty string. That is a business decision. Your mapping should reflect the behavior your application expects.

Also review custom mapping methods and nullable reference types. Mapperly exposes diagnostics for some nullable mapping situations, but not every runtime data condition can be resolved at compile time.

Step 7: Check IQueryable Projections and Generated SQL

If your application uses AutoMapper's ProjectTo(), don't replace it with a normal object mapping call without reviewing the query.

Mapperly supports IQueryable projections.

For example:

[Mapper]
public static partial class InvoiceMapper
{
    public static partial IQueryable<InvoiceDto> ProjectToDto(
        this IQueryable<Invoice> query
    );
}

You can then use the projection with Entity Framework:

var invoices = await DbContext.Invoices
    .Where(x => x.IsActive)
    .ProjectToDto()
    .ToListAsync();

This approach allows the projection to be translated into an expression that an ORM can use.

However, queryable projections have limitations. Some runtime mapping features, reference handling, and custom mapping behavior are not available in the same way.

After converting a projection:

  1. Compare the old and new query results.
  2. Inspect the generated SQL.
  3. Check which columns are retrieved.
  4. Test filtering, sorting, and pagination.
  5. Confirm that tenant filters remain applied.

This is especially important in multi-tenant applications, where a query change can affect which records are returned.

Step 8: Run Tests Against Multiple Tenants

A mapping problem doesn't always produce an exception.

Sometimes the application returns a DTO with missing information or includes a record that should not be visible to the current tenant.

For tenant-scoped mappings, create test data for at least two tenants.

Then verify:

  • Tenant A only receives its own records.
  • Tenant B only receives its own records.
  • Related entities belong to the expected tenant.
  • Query projections preserve the original filters.
  • Background jobs and event handlers use the correct tenant context when creating or mapping DTOs.

Mapping itself does not replace tenant authorization or query filtering. Those protections must remain in the application and data-access layers.

The objective is to confirm that the migration hasn't changed the behavior around tenant data. For a worked example of this kind of check, see how to catch a tenant data leak in ASP.NET Zero code.

Step 9: Remove Unused AutoMapper Code and Dependencies

Once your solution has moved to Mapperly, search the entire repository again.

Check for:

  • Remaining CreateMap configurations.
  • Old AutoMapper profile classes.
  • [AutoMapFrom] and [AutoMapTo] attributes.
  • Unused custom converters and resolvers.
  • AutoMapper package references.
  • Old ABP module dependencies.
  • Tests that still expect the previous mapping behavior.

Don't remove a package simply because the main Application project no longer references it. Another project may still depend on it.

Run a complete build and test suite after removing the remaining references.

If you are keeping AutoMapper for a specific legacy integration, document why it remains, which project owns it, and how its compatibility and licensing are maintained.

AutoMapper vs Mapperly: Common Migration Changes

Here is a quick reference for developers converting existing mapping configurations.

AutoMapper Mapperly
CreateMap<TSource, TDestination>() A [Mapper] class with a partial mapping method
.ForMember(...).Ignore() [MapperIgnoreTarget(...)]
.ForMember(...).MapFrom(...) [MapProperty(...)]
.ReverseMap() Define the reverse mapping explicitly
ProjectTo<TDestination>() An IQueryable projection method
AfterMap(...) A suitable user-defined mapping method or application-service logic
Runtime configuration validation Compile-time analyzer diagnostics

These are conceptual equivalents, not guaranteed one-to-one replacements. Review each mapping's behavior before converting it.

Common Mistakes During an ASP.NET Zero Mapperly Migration

A few shortcuts can make the migration look faster while creating more work later.

1. Replacing Packages Before Checking Dependencies

Removing AutoMapper before identifying every profile, converter, and project reference can leave the application in a difficult state.

2. Assuming Every Mapping Is a Simple Property Copy

Custom conversion rules and nested navigation properties often contain behavior that isn't obvious from the DTO.

3. Ignoring Mapperly Warnings

A warning may represent a property that your application relies on. Suppressing it without investigation can hide a real issue.

4. Combining ABP Mapper Integrations Without Checking Compatibility

The AutoMapper and Mapperly ABP modules both provide the object mapper integration. Don't register both as though they were independent mapping services.

5. Testing Only with One Tenant

A successful test using one tenant does not prove that tenant-scoped queries still behave correctly.

6. Treating AI-Generated Mappings as Production-Ready

AI tools can help convert repetitive CreateMap configurations into Mapperly classes. But they cannot reliably infer every business rule, null-handling decision, or tenant boundary from a mapping declaration alone.

Review generated code and test AI-generated code before you merge it.

Frequently Asked Questions

Do I have to remove AutoMapper when upgrading to ASP.NET Zero v15?

ASP.NET Zero's default mapping integration has moved to Mapperly. If your application still depends on AutoMapper, you need to assess those dependencies and choose a compatible migration or retention strategy.

Do not assume that you can register both ABP mapper modules together. Check the integration guidance for your target version.

Does ObjectMapper.Map() still work with Mapperly?

Yes. ASP.NET Zero's Mapperly integration preserves the ObjectMapper.Map() API for supported mappings. You can therefore keep many existing mapping calls while converting the underlying mapping definitions.

Is Mapperly faster than AutoMapper?

Mapperly generates mapping code at compile time and avoids runtime reflection for its generated mappings. This can reduce runtime overhead.

Actual performance depends on your mapping complexity, application workload, and implementation. Benchmark important paths rather than assuming a fixed performance improvement.

Does Mapperly support ProjectTo?

Mapperly supports IQueryable projections through generated projection methods. However, these projections have limitations compared with normal object mappings, so review the supported features and inspect the generated SQL.

Can AI tools migrate AutoMapper profiles to Mapperly?

AI coding tools can help convert repetitive mapping configurations, generate mapper classes, and identify possible missing properties.

They should be treated as migration assistants, not as proof that the conversion is correct. Review ignored properties, custom converters, null handling, and tenant-scoped queries manually.

How should I test an AutoMapper to Mapperly migration?

Start with mapping unit tests, then add integration tests for nested objects, null values, query projections, and tenant-specific records. Finally, run the complete application test suite against the upgraded ASP.NET Zero solution.

A Successful Build Doesn't Guarantee a Safe Migration

Moving from AutoMapper to Mapperly is not just a framework-level change. It affects how your application translates entities, DTOs, and query results.

The safest approach is to understand your existing mappings first, convert them in a controlled order, and test the behavior that the compiler cannot verify.

Start with simple DTOs. Review custom rules. Make important diagnostics fail the build. Test null relationships and tenant-specific queries. Then remove the old dependencies when the application is ready.

Because a mapping bug doesn't always crash your application. Sometimes it quietly returns incomplete data.

And in a multi-tenant application, that is a risk worth catching before release.

Working on an ASP.NET Zero v15 upgrade? ASP.NET Zero Developers helps teams review customized applications, identify migration risks, and plan framework upgrades without overlooking the code that makes their applications unique.

We have been building .NET and ASP.NET Zero software for clients in the US and UK since 2018. Take a look at our recent work, or book a free consultation to talk through your upgrade.


Further reading:

Get In Touch

Ready to start your ASP.NET Zero project?

Hire ASP.Net Zero Application Developers that will provide the perfect solution to your business issues. Our technical experts will provide you with a free consultation.

More from the Blog