question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug in using Include to map derived classes

See original GitHub issue

Source/destination types

public class Destination 
{
   public string Field { get; set; }
}

public class Source
{
}

public class DerivedSource : Source
{
}

Mapping configuration

CreateMap<DerivedSource, Destination>();

CreateMap<Source, Destination>()
  .Include<DerivedSource, Destination>()
  .ForMember(dest => dest.Field, opts => opts.Ignore());

Version: 8.1.0

Expected behavior

In AutoMapper 6.2.2, mapping inheritance worked regardless of ordering of CreateMap calls. However, after upgrading to version 8 we have to make sure that any derived types are declared after the base class which Includes these same base types. The above example fails (AssertConfigurationIsValid in the MapperConfiguration throws an AutoMapperConfigurationException) because Include<DerivedSource, Destination>() is done after CreateMap<DerivedSource, Destination>(). If the maps are created in opposite order (i.e. do CreateMap<Source, Destination>() first) the mapping works as intended. Interestingly, ordering does not appear to be relevant when using IncludeBase . This seems to indicate that this is a bug with Include, and not an intentional (breaking) change with version 8?

Actual behavior

AutoMapper.AutoMapperConfigurationException : Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters DerivedSource -> Destination (Destination member list)

Unmapped properties: Field3

Steps to reproduce

// Your calls to Mapper.Map or ProjectTo here, with source/destination objects constructed

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
lbargaoanucommented, May 7, 2019

Thanks. That looks like a bug indeed.

0reactions
lock[bot]commented, Jun 10, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AutoMapper Map From Base Class Include Derived Class
Need some expert advice on this Automapper problem I'm trying to solve. I have three classes, an entity, a base, and a derived...
Read more >
Question/problem with mapping a derived type to base type
If I map a derived class instance to a base class instance, it still returns me an instance of the derived class. Is...
Read more >
How can I store derived classes in a map?
The map has to comply to a common interface or base class, so all elements have a valid cast for it. Somehow it...
Read more >
Saving data from derived class in map - Forum
I have a map of a base class and I'm using it to save all of the instances of the derived classes. When...
Read more >
Is this a bug in MinGW compiler? - Forum
I'd like to add that I use class templates, not only classes. It just acts like this variable doesn't exist. In file included...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found