I would like to ignore some properties when displaying deep (i.e. levels> 1) object models.
The following test works fine:
class Foo { public string Text { get; set; } } class Bar { public string Text { get; set; } } Mapper.CreateMap<Foo, Bar>() .ForMember(x => x.Text, opts => opts.Ignore()); var foo = new Foo { Text = "foo" }; var bar = new Bar { Text = "bar" }; Mapper.Map(foo, bar); Assert.AreEqual("bar", bar.Text);
However, when I try to do the same mapping, but have the Foo and Bar properties as properties of the parent class, the following test fails:
class ParentFoo { public Foo Child { get; set; } } class ParentBar { public Bar Child { get; set; } } Mapper.CreateMap<ParentFoo, ParentBar>(); Mapper.CreateMap<Foo, Bar>() .ForMember(x => x.Text, opts => opts.Ignore()); var parentFoo = new ParentFoo { Child = new Foo { Text = "foo" } }; var parentBar = new ParentBar { Child = new Bar { Text = "bar" } }; Mapper.Map(parentFoo, parentBar); Assert.AreEqual("bar", parentBar.Child.Text);
Instead of ignoring the text of the Child class (that is, leaving it as "bar"), automapper sets the value to null. What am I doing wrong with my display configuration?
Brownie
source share