Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 339 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Auto Mapper Unmapped members were found

#1
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.
Reply

#2
it is about validation.

cfg.ValidateInlineMaps = false;

should be enough
Reply

#3
Quick intro edit: as @mrTurkay answers below, this can be solved with the following configuration:

cfg.ValidateInlineMaps = false;

However, you should understand why the problem occours in the first place - so feel free to read on.

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 a `CarDTO.cs`

The `Car.cs` looks like this:

public class Car
{
public string Color { get; set; }

public Engine Engine { get; set; }
}

And your DTO looks the same, but has a reference to the `EngineDTO` instead:

public class CarDTO
{
public string Color { get; set; }

public EngineDTO Engine { get; set; }
}

You configured the mapping like this:

Mapper.CreateMap<DTO.CarDTO, Data.Model.Car>();
Mapper.CreateMap<Data.Model.Car, DTO.CarDTO>();

Mapper.CreateMap<DTO.EngineDTO, Data.Model.Engine>();
Mapper.CreateMap<Data.Model.Engine, DTO.EngineDTO>();

All looks fine, right? However, in your `EngineDTO`, you probably have a navigation property like, lets say:

public class EngineDTO
{
public List<PartDTO> Parts { get; set; }
}

So while Automapper is Mapping from `Engine` to `EngineDTO`, it also tries to Map the `PartDTO`, but since you forgot to declare the mapping in the `global.asax`, you get the error:

> 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

If you don't want to map certain properties on a class, you can use Ignore:

Mapper.CreateMap<Engine, EngineDTO>()
.ForMember(x => x.Parts, opt => opt.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:

public class CarProfile : Profile
{
public CarProfile ()
{
CreateMap<Car, CarDTO>();
}
}

Global.asax:

Mapper.Initialize(cfg =>
{
cfg.AddProfile<CarProfile>();
}
Reply

#4
In my case, I had forgotten to add Map Configuration to MapConfig.cs.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through