Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9028753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:04:15+00:00 2026-06-16T07:04:15+00:00

Can I use inheritance mapping in AutoMapper (v2.2) for maps with the same Source

  • 0

Can I use inheritance mapping in AutoMapper (v2.2) for maps with the same Source type but different Destination types?

I have this basic situation (the real classes have many more properties):

public abstract class BaseViewModel
{
    public int CommonProperty { get; set;}
}

public class ViewModelA : BaseViewModel
{
    public int PropertyA { get; set; }
}

public class ViewModelB : BaseViewModel
{
    public int PropertyB { get; set; }
}

ViewModelA and ViewModelB are different representations of the same Entity class:

public class Entity
{
    public int Property1 { get; set; }
    public int Property2 { get; set; }
    public int Property3 { get; set; }
}

I want to reuse the same mapping for BaseViewModel for each ViewModel, such as:

Mapper.CreateMap<Entity, BaseViewModel>()
    .Include<Entity, ViewModelA>()
    .Include<Entity, ViewModelB>()
    .ForMember(x => x.CommonProperty, y => y.MapFrom(z => z.Property1));

Mapper.CreateMap<Entity, ViewModelA>()
    .ForMember(x => x.PropertyA, y => y.MapFrom(z => z.Property2));

Mapper.CreateMap<Entity, ViewModelB>()
    .ForMember(x => x.PropertyB, y => y.MapFrom(z => z.Property3));

But unfortunately, this doesn’t seem to work. Calls like these:

var model = Mapper.Map<Entity, ViewModelA>(entity);

result in model having PropertyA mapped, but not CommonProperty. I believe I’m following the examples in https://github.com/AutoMapper/AutoMapper/wiki/Mapping-inheritance properly, but I’m afraid having multiple maps created with the same Source type is tripping AutoMapper up.

Any insights? I love the idea of grouping Base class mappings together, but this doesn’t seem to work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:04:17+00:00Added an answer on June 16, 2026 at 7:04 am

    Unfortunately in this case, AutoMapper seems to be registering only one child class mapping per source type, the last one (ViewModelB). This was probably designed to work with parallel hierarchies, not with a single source type.

    To work around this, you can encapsulate the common mappings in an extension method:

    public static IMappingExpression<Entity, TDestination> MapBaseViewModel<TDestination>(this IMappingExpression<Entity, TDestination> map)
      where TDestination : BaseViewModel { 
      return map.ForMember(x => x.CommonProperty, y => y.MapFrom(z => z.Property1));
    }
    

    And use it in the individual subclass mappings:

    Mapper.CreateMap<Entity, ViewModelA>()
      .MapBaseViewModel<ViewModelA>()
      .ForMember(x => x.PropertyA, y => y.MapFrom(z => z.Property2));
    
    Mapper.CreateMap<Entity, ViewModelB>()
      .MapBaseViewModel<ViewModelB>()
      .ForMember(x => x.PropertyB, y => y.MapFrom(z => z.Property3));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you can do this with inheritance, but you're meant to use inheritance
With Yii php framework, I use inheritance. In my AbstractModel, I have this method:
I want to use inheritance to have an object treated in a different way
I have an entity which defines inheritance like this: * @DiscriminatorColumn(name=type, type=string) * @DiscriminatorMap({text
I have some object classes that use inheritance. It seems that I can only
I can use this maven plugin maven-jaxb-plugin to generate Java Classes from XSD file.
I can use .button() to create beautiful submit buttons, but the rest of the
I just read this nice article that taught me how to use inheritance (Table-per-hirarchy).
In my current project I have a inheritance structure that looks like this: @Entity
I'm building a Rails application with multiple roles. I can't use inheritance against the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.