We are using Automapper for a project, and seem to get the following error randomly:
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
The code hasn't been changed in months. I get that error, refresh and the error is gone and page works fine. I'm using
Mapper.AssertConfigurationIsValid();
not sure why it complains the mappings are not good and then a refresh and it's fine again, has anyone run into this? Debugging doesn't help as it's random, sometimes no errors and then other days it will popup somewhere on the site, come back to it and it's fine. The error also comes up on random pages, not the same page, not the same mapping.
This problem occours when you're trying to map an object that you didn't create a mapping configuration for. What you need to keep in mind is, that it doesn't have to be the specific object you're trying to map; but one of it's navigation properties.
Say for instance you have a
Car.cs
that you want to map to aCarDTO.cs
The
Car.cs
looks like this:And your DTO looks the same, but has a reference to the
EngineDTO
instead:You configured the mapping like this:
All looks fine, right? However, in your
EngineDTO
, you probably have a navigation property like, lets say:So while Automapper is Mapping from
Engine
toEngineDTO
, it also tries to Map thePartDTO
, but since you forgot to declare the mapping in theglobal.asax
, you get the error:If you don't want to map certain properties on a class, you can use Ignore:
EDIT:
For a more robust AutoMapper configuration, I suggest that you use mapping profiles, instead of declaring the mapping directly in
Global.asax
. Here is an Example:Profile:
Global.asax:
it is about validation.
should be enough