Reputation: 2698
I'm trying to use the AfterMap
method to flatten the input class to output. The input classes look as following:
public class FooDTO {
public string SomeFieldA { get; set; }
public string SomeFieldB { get; set; }
....
public BarDTO SomeFieldY { get; set; }
public BazDTO SomeFieldZ { get; set; }
}
public class BarDTO {
public string SomeOtherFieldA { get; set; }
}
public class BazDTO {
public string YetAnotherFieldA { get; set; }
}
and the output class as following:
public class Foo {
public string SomeFieldA { get; set; }
public string SomeFieldB { get; set; }
....
public string SomeOtherFieldA { get; set; }
public string YetAnotherFieldA { get; set; }
}
My AutoMapper mapping configuration looks as following:
CreateMap<FooDTO, Foo>()
.AfterMap((src, dest, ctx) => ctx.Mapper.Map(src.SomeFieldY, dest))
.AfterMap((src, dest, ctx) => ctx.Mapper.Map(src.SomeFieldZ, dest));
CreateMap<BarDTO, Foo>();
CreateMap<BazDTO, Foo>();
My problem is that when I try to run my application, I get the following unhandled exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.MissingMethodException: Method not found: 'AutoMapper.IMappingExpression`2<!0,!1> AutoMapper.IMappingExpression`2.AfterMap(System.Action`3<!0,!1,AutoMapper.ResolutionContext>)'.
Am I missing some other configuration? How do I make my code work?
Upvotes: 5
Views: 5755
Reputation: 2698
As Lucian Bargaoanu mentioned, it was caused by mismatch of versions of Nuget packages. Once I upgraded another project to version 9.0.0, it started to work.
Upvotes: 7