Reputation: 592
I know there are a lot of questions (and answers) about this BUT none of these works for me when using .net6 and automapper 11.01.1
They seem to have removed many of these Ignore
, IgnoreAllUnmapped
and ForAllOtherMembers
in the latest automapper.
If I use ignore with ForAllMembers
(before or after ForMember
) it will ignore all fields, even those I specify with a map.
The problem: I have two classes with fields with the same name, but I only want to map a few and ignore the rest. (please don't say "why do you need automapper" that's not the question here).
I need to use automapper in this case but not sure if they support this anymore? Am I missing a nuget maybe? I only use the "AutoMapper 11.01.1"
public class User1
{
public string Name { get; set; } = "Foo";
public int Age { get; set; } = 7;
public string Phone { get; set;} = "123456789";
}
public class User2
{
public string FirstLastName { get; set; }
public int Age { get; set; }
public string Phone { get; set; }
}
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
CreateMap<User1, User2>()
.ForMember(dest => dest.FirstLastName, opt => opt.MapFrom(src => src.Name))
//.ForMember(dest => dest.Age, src => src.Ignore()); // works BUT I do not want to ignore every field manually
//.ForAllMembers(dest => dest.Ignore()) // doesn't work, clears all fields
//.ValidateMemberList(MemberList.None) // doesn't work
;
}
}
void Main()
{
var user1 = new User1();
var config = new MapperConfiguration(mc => mc.AddProfile(new AutoMapperProfile()));
Mapper mapper = new Mapper(config);
var user2 = mapper.Map<User2>(user1);
user2.Dump();
}
Upvotes: 14
Views: 8606
Reputation: 109
I changed previous answer of Pullen. Great thanks to him.This is a method for ignore non existing in the destination properties.
public static IMappingExpression<TSource, TDestination> IgnoreNonExistingMembers<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expr)
{
var sourceType = typeof(TSource);
var destinationType = typeof(TDestination);
foreach (var property in destinationType.GetProperties())
{
if (sourceType.GetProperty(property.Name) != null)
continue;
expr.ForMember(property.Name, opt => opt.Ignore());
}
return expr;
}
Upvotes: 2
Reputation: 196
We ran into the same issue. I created this extension method which should provide the functionality you're looking for.
public static IMappingExpression<TSource, TDestination> IgnoreAllMembers<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expr)
{
var destinationType = typeof(TDestination);
foreach (var property in destinationType.GetProperties())
expr.ForMember(property.Name, opt => opt.Ignore());
return expr;
}
The usage for it:
CreateMap<ModelOne, ModelTwo>()
.IgnoreAllMembers()
.ForMember(x => x.DescriptionOne, opt => opt.MapFrom(y => y.DescriptionOne));
It works by looping through all the properties of the destination type and ignoring them. This means it needs to be called before you provide your member mappings. Calling it after will override your mappings.
Hope this helps.
Upvotes: 18