11

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();
}
kaya3
  • 47,440
  • 4
  • 68
  • 97
steb
  • 436
  • 3
  • 12
  • Does this answer your question? [How to ignore all destination members, except the ones that are mapped?](https://stackoverflow.com/questions/4367591/how-to-ignore-all-destination-members-except-the-ones-that-are-mapped) – Markus May 24 '22 at 17:54
  • 2
    Unfortunately no, I don't think IgnoreAllUnmapped still exists in automapper, unless I'm missing some nuget or using the wrong nuget. – steb May 24 '22 at 18:08
  • Possible solution is in https://stackoverflow.com/questions/71311303/replacement-for-automappers-forallothermembers/74394201#74394201 – Michael Freidgeim Jan 06 '23 at 18:53
  • Does this answer your question? [AutoMapper: "Ignore the rest"?](https://stackoverflow.com/questions/954480/automapper-ignore-the-rest) – Ian Kemp Apr 21 '23 at 11:24

2 Answers2

14

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.

Pullen
  • 156
  • 2
  • 3
  • 3
    Thats cool, thank you for the respond. I guess there are no ways of doing this with automapper any more, and that's sooooo strange. – steb Aug 17 '22 at 15:02
  • 2
    @steb you're much more polite about it than I was when I found about this when trying to upgrade solely to get .NET 7 working. Especially annoyed considering my code had been working just fine for 3 years. – Simon_Weaver Nov 12 '22 at 06:56
  • Yea same here, it wad not a piece of cake and update the nugets but now it’s done and no need to look back – steb Nov 13 '22 at 09:38
  • Thanks. Mapping was working before and now I have to use above solution to ignore other properties mapping being done automatically for some reason. – sanpat Feb 08 '23 at 20:03
2

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;
}
Alexei
  • 101
  • 1
  • 2