3
votes

I have these 2 classes:

public class MyModel()
{
    public int Id {get; set;}
    public string Name {get; set;}
    public bool IgnoreModel {get; set;}
}

public class MyDto()
{
    public int Id {get; set;}
    public string Name {get; set;}
    public bool IgnoreDto {get; set;}
}

I want to ignore both IgnoreModel and IgnoreDto. My mapping looks like this:

Mapper.CreateMap<MyModel, MyDto>().Bidirectional()
.ForMember(model=> model.IgnoreModel, option => option.Ignore())
.ForSourceMember(dto => dto.IgnoreDto, option => option.Ignore());

Where Bidirectional() is an extension:

    public static IMappingExpression<TDestination, TSource> Bidirectional<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
    {
        return Mapper.CreateMap<TDestination, TSource>();
    }

However, I get an error that IgnoreDto is not mapped:

`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

MyModel -> MyDto (Destination member list) ModelAssembly.MyModel -> DtoAssembly.MyDto (Destination member list)

Unmapped properties: IgnoreDto`

What is the correct way to ignore this kind of mapping?

1
That looks right to me. What's the output of Mapper.AssertConfigurationIsValid()? Also, out of curiousity, does dropping Bidirectional() and manually mapping model->dto and dto-> model make any difference?Nate Barbettini
Where does the Bidirectional method come from?Andrew Whitaker
@AndrewWhitaker It's an extension I wrote.Ivan-Mark Debono

1 Answers

3
votes

Applying your extension (which returns another object) after the mapping is making your mapping rules apply on the second mapping. Your first mapping is left with no rules applied.

To make it work, use ignore on each mapping separately:

Mapper.CreateMap<MyModel, MyDto>()
.ForMember(model=> model.IgnoreModel, option => option.Ignore());

Mapper.CreateMap<MyDto, MyModel>()
.ForMember(model=> model.IgnoreDto, option => option.Ignore());