Automapper raised Error mapping types on upgrade to 11.0.0 (List to Dictionary) - automapper

I have a problem on map of property with type List to Dictionary.
this code was success on Automapper 10.1.1 and error raised after upgrade to 11.0.0.
CreateMap<ProcessActorEntity, ProcessActorViewModel>()
.ForMember(dest => dest.Roles,
opt => opt.MapFrom(src => src.ProcessRoles.ToDictionary(x => x.Role.Id, x => x.Role.Title)));
Roles is a List of role entity.
ProcessRoles a is Dictionary.
It's work on AutoMapper V10.1.1, and Not work on V11.0.0
Error mapping types ... Destination Member: Roles

I found the error on the run-time:
In Automapper V10.1.1, no error raised, if Role be null
But in Version V11+, Automapper raised error, is Role is null.
Role is null and It's our fault. but Automapper has different approach in versions.
Role is null and Automapper can not access the Id and Title members.

Related

AutoMapper 8 Upgrade ConstructUsing

I have just upgraded to v8 of AutoMapper and the expression below fails but I really cannot see why.
cfg.CreateMap<string, Nmtoken>()
.ConstructUsing(i => new Nmtoken(i))
.ForMember(m => m.Token, o => o.MapFrom(s => s));
I believe that the passed expressions are valid. I get no build errors and intellisense is happy. But I get runtime error:
System.MissingMethodException: 'Method not found:
'AutoMapper.IMappingExpression`2<!0,!1>
AutoMapper.IMappingExpression`2.ConstructUsing(System.Func`2<!0,!1>)'.'
I'm happy to admit that I don't understand the error message.
Apologies. I have two projects linked with a dependency and one of them was still using AutoMapper 7.
I had the samme issue, but i was using AutoMapper v8.0.0.
Upgrading to version v8.1.0 solved the issue.

Automapper map one field differently

I have a class that I want to map to another. All the fields have the same name in both classes, except one.
Is there a quick way to do that, without having to use ForMember for all fields?
Thank you.
Found a solution :
cfg.CreateMap<MasterA, MasterB>().ForMember
(dest => dest.IdOld, opt => opt.MapFrom(src => src.Id));

automapper mapping a indexer

I am trying to write a mapping to map between two classes using automapper. Most of it is pretty straight forward, direct mappings between 2 fields of the same type. However, I have an indexer on each class that may that need to map to each other. It probably isn't relevant that the source type has an indexer, so essentially what I am trying to do is something like:
mappingExpression.ForMember(d => d["Text"], opt => opt.MapFrom(s => s.Text));
Which gives me the error:
Custom configuration for members is only supported for top-level individual members on a type.
Is there any way of achieving this?

Automapper Exception. Unmapped members were found

I have the following inner exception being returned from automapper:
AutoMapper.AutoMapperConfigurationException was unhandled by user code
HResult=-2146233088
Message= 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
CreateAndEditCustomer -> Customer (Destination member list)
App.Web.Mvc.Models.Customer.CreateAndEditCustomer ->
App.Model.Customer (Destination member list)
Deleted
Does anyone know what Deleted means?
How can I discover which members have not been mapped?

Automapping Lists

I'm starting to learn AutoMapper and coming up against a couple of minor problems.
Essentially I'm getting null reference exceptions when trying to bind to ILists produced by AutoMapper.
My boot strapping method looks like this:
Mapper.CreateMap<Claimant, ClaimantViewModel>()
.ForMember(
vm => vm.Check,
opt => opt.Ignore());
Mapper.CreateMap<IList<Claimant>, IList<ClaimantViewModel>>();
Mapper.AssertConfigurationIsValid();
Which doesn't look to fancy to me. I then try to call:
dlWAMs.DataSource = Mapper.Map<IList<Claimant>, IList<ClaimantViewModel>(someilist);
dlWAMs.DataBind();
With that I'm getting a null reference exception. If I code my own loop and map the models to a view model one at a time the code runs fine.
What am I doing wrong?
First of all you don't need that second map that creates map from IList to IList, remove it. Than if it does not work, show us your classes.

Resources