Open Closed

ABP Studio 2.1.9 Bug: Code Generation Duplicates Mapperly Mappers in Projects Migrated from AutoMapper #10402


User avatar
0
granade created

Environment

  • ABP Studio Version: 2.1.9
  • ABP Framework Version: 10.0.2
  • Project Type: Blazor Server
  • Mapping Library: Mapperly (migrated from AutoMapper using official migration guide)

Summary

When using ABP Studio to regenerate an entity in a project that was migrated from AutoMapper to Mapperly (following https://abp.io/docs/10.0/release-info/migration-guides/AutoMapper-To-Mapperly), the code generator appends duplicate mapper classes to the bottom of ApplicationMappers.cs instead of detecting and updating the existing Mapperly mapper definitions.

This causes the Mapperly source generator to crash, which cascades into dozens of compilation errors.

Steps to Reproduce

  1. Start with an ABP project that originally used AutoMapper (e.g., created with ABP Suite pre-10.0)
  2. Migrate to Mapperly following the official migration guide linked above
  3. Generate an entity (e.g., Product) with all options enabled — verify the project compiles
  4. Open the same entity in ABP Studio and regenerate it (even a minor change like unchecking Excel Export)
  5. Build the solution

What Happens

ABP Studio appends new mapper classes at the bottom of [ProjectName]ApplicationMappers.cs, even though Mapperly mappers for the same entity already exist earlier in the file. For example, the file ends up with both:

// Original (already existed, ~line 50)
[Mapper(RequiredMappingStrategy = RequiredMappingStrategy.Target)]
public partial class ProductToProductDtoMapper : MapperBase<Product, ProductDto>
{
    public override partial ProductDto Map(Product source);
    public override partial void Map(Product source, ProductDto destination);
}

// Duplicate appended by ABP Studio (~line 200)
[Mapper]
public partial class ProductToProductDtoMappers : MapperBase<Product, ProductDto>
{
    public override partial ProductDto Map(Product source);
    public override partial void Map(Product source, ProductDto destination);
}

Note: some duplicates use a slightly different class name (trailing "s"), while others use the exact same class name, producing different error types.

This causes the Mapperly source generator to fail entirely with:

CS8785: Generator 'MapperGenerator' failed to generate source. Exception was of type 'InvalidOperationException' with message 'Sequence contains more than one element'

Because the source generator crashes, none of the ~100 mapper classes in the file receive their generated implementations, producing widespread CS8795 errors ("Partial method must have an implementation part because it has accessibility modifiers").

Additional Issue (same regeneration action)

When unchecking features like "Create user interface" and regenerating, ABP Studio correctly removes those methods from the base interface (e.g., IProductsAppService.cs). However, it does not update or warn about .Extended files that override or call the removed methods, causing additional CS0115 and CS1061 errors.

Expected Behavior

  1. ABP Studio should detect existing Mapperly mapper classes and update them in-place (or skip if unchanged), not append duplicates
  2. When removing methods from the base interface/service, ABP Studio should either update .Extended files or warn the developer about broken references

Suspected Root Cause

I believe ABP Studio's code generator may be looking for AutoMapper-style CreateMap<>() calls in a Profile class to determine whether mappers exist. In a migrated project, those calls have been replaced by Mapperly MapperBase<> classes, which the generator doesn't recognize — so it treats the entity as having no mappers and generates new ones.

Projects created fresh with ABP 10.0+ (where Mapperly is the default) likely don't have this issue because the generator was designed to work with the mapper format it created.

Workaround

Manually revert ApplicationMappers.cs from source control to remove the appended duplicates, then manually fix any .Extended files referencing removed methods.


1 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    • This is a known ABP Studio code-generation limitation/bug in Studio 2.1.9 for solutions that were migrated from AutoMapper to Mapperly: during regeneration, Studio fails to recognize the existing Mapperly mapper blocks in *ApplicationMappers.cs and appends new mapper classes instead of updating/reusing the existing ones.

      • Result: duplicate [Mapper] classes for the same (TSource, TDestination) pair → Mapperly generator throws CS8785 (Sequence contains more than one element) and then many follow-up partial-method errors.
    • Workaround (safe + minimal changes):

      1. After every regeneration, revert the mapper file(s) from source control (as you already do), or manually delete the appended duplicates in *ApplicationMappers.cs.
      2. To prevent the Mapperly generator from crashing while you’re cleaning up, ensure there is exactly one MapperBase<TSource, TDestination> (or TwoWayMapperBase<,>) for each mapping pair across the project.
      3. If Studio occasionally appends with a slightly different name (e.g., trailing s), you still must delete one of them—the Mapperly failure is about duplicated mapping pairs, not just class names.
    • About .Extended files after feature removal:

      • Studio updates the “generated/base” interfaces/classes, but it does not automatically refactor your manually-maintained .Extended files.
      • Workaround: treat .Extended files as manual code: after regeneration, remove/adjust overrides and calls to the deleted methods, or revert those .Extended files too (depending on what you want to keep).
    • Best next step (so this can be fixed):

      • Report it to Volosoft via the official channel and include:
        • Studio version (2.1.9), Framework version (10.0.2)
        • A before/after ApplicationMappers.cs diff showing the duplicated mapper blocks
        • The CS8785 error text
      • Please contact: https://abp.io/contact

    (Your migration-guide link is reachable and matches the scenario you described: https://abp.io/docs/10.0/release-info/migration-guides/AutoMapper-To-Mapperly)


    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.2.0-preview. Updated on February 05, 2026, 13:24
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.