I'd like to be able to do something like this using automapper:
Mapper.CreateMap<Source, Destination>()
.ForMember<d => d.Member, "THIS STRING">();
I'd like d.Member to always be "THIS STRING" and not be mapped from any particular member from the source model. Putting a string field in the source model with "THIS STRING" as it's value is also not an option.
Does AutoMapper support these kinds of things in any way?
Mapper.CreateMap<Source, Destination>()
.ForMember(dest => dest.Member, opt => opt.UseValue<string>("THIS STRING"));
Starting with version 8.0 you have to use the following:
Mapper.CreateMap<Source, Destination>()
.ForMember(dest => dest.Member, opt => opt.MapFrom(src => "THIS STRING"));
Related
Automapper version 8.0.0 removed ResolveUsing extension method from IMappingExpression and consolidated it with MapFrom extension method. However, after replacing ResolveUsing with MapFrom method, certain configurations throw exception.
Original ResolveUsing:
CreateMap<SourceType, DestinationType>()
.ForMember(dest => dest.Customer,
opt => opt.ResolveUsing(src => src?.Customer ?? new Customer())
);
Replaced with MapFrom:
CreateMap<SourceType, DestinationType>()
.ForMember(dest => dest.Customer,
opt => opt.MapFrom(src => src?.Customer ?? new Customer())
);
This produces compilation error:
Error CS8072
Automapper
An expression tree lambda may not contain a null propagating operator.
New Func-based overloads in Automapper 8.0.0 accept more parameters compared to old/removed ResolveUsing overloads.
Instead of using lambda expression with single input parameter opt.MapFrom(src => ...) when replacing ResolveUsing, overload with 2 parameters should be used opt.MapFrom((src, dest) => ...).
MapFrom expression becomes:
opt => opt.MapFrom((src, dest) => src?.Customer ?? new Customer())
Full example:
CreateMap<SourceType, DestinationType>()
.ForMember(dest => dest.Customer,
opt => opt.MapFrom((src, dest) => src?.Customer ?? new Customer())
);
I am a bit new to AutoMapper, I am trying to map a property where the values from from an ef db context.
How would I go about doing that?
CreateMap<User, UserDto>()
.ForMember(dest => dest.Username, opt => opt.MapFrom(src => src.Username))
.ForMember(dest => dest.NetworkId, opt => opt.MapFrom(src => src.NetworkId))
.ForMember(dest => dest.Network, /*NEED TO TAKE NetworkId and PERFORM A lookup in DbContext AND SET Network property*/);
I'm trying to map List to List but one of the properties of the DtoLineItem is different.
How can I tell AutoMapper to map a specific property of LineItem to that Property?
I've tried this:
CreateMap<Cart, CartModel>()
.ForMember(dest => dest.LineItems, opt => opt.MapFrom(src => src.LineItems))
.ForMember(dest => dest.LineItems.Select(x => x.CustomField),
opt => opt.MapFrom(src => src.LineItems.Select(y => y.Description)));
AutoMapper.AutoMapperConfigurationException Custom configuration for
members is only supported for top-level individual members on a type.
Let say I have mapping like this:
Mapper.CreateMap<CalendarEvent, CalendarEventForm>()
.ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.EventDate.Date))
Is it possible to get back the destination member expression by its relative source expression. Something like this:
Expression<Func<CalendarEventForm, object>> destinationExpression = Mapper.GetMemmberExpression(src => src.EventDate.Date);
Thanks in advance.
I have ran into an issue where I am trying to ignore properties within properties. e.g.
Mapper.CreateMap<Node, NodeDto>()
.ForMember(dest => dest.ChildNodes, opt => opt.Ignore())
.ForMember(dest => dest.NodeType.EntityType.Properties, opt => opt.Ignore());
I get following exception:
{"Expression 'dest => dest.NodeType.EntityType.Properties' must resolve to top-level member.\r\nParameter name: lambdaExpression"}
Any idea?
Well I have managed to figure it out by myself. I have to specify the nested property options in its own dto mapping. However let me know if there is another better way of doing this
Mapper.CreateMap<EntityType, EntityTypeDto>()
.ForMember(dest => dest.Properties, opt => opt.Ignore());
Mapper.CreateMap<Node, NodeDto>()
.ForMember(dest => dest.ChildNodes, opt => opt.Ignore());