AutoMapper:将Child Collection 1映射到Child Collection2时丢失未映射的属性值

时间:2013-01-09 06:52:02

标签: c# nested automapper

使用AutoMapper:在映射嵌套集合时,我希望任何未映射的属性都保留其原始值。相反,它们被设置为null。

示例:
我有这四个班 (请注意,Test2Child具有Name属性,而Test1Child则不具有此属性:

public class Test1
{
    public List<Test1Child> Children { get; set; }
}
public class Test2
{
    public List<Test2Child> Children { get; set; }
}
public class Test1Child
{
    public int Value { get; set; }
}
public class Test2Child
{
    public string Name { get; set; }
    public int Value { get; set; }
}

...以及简单的映射设置。

Mapper.CreateMap<Test1, Test2>();
Mapper.CreateMap<Test1Child, Test2Child>().ForMember(m => m.Name, o => o.Ignore());
Mapper.AssertConfigurationIsValid();    // Ok

我希望在映射期间保留Test2Child.Name的原始值....我希望这是任何未映射属性的默认行为。

当我直接从Test1Child映射到Test2Child时,它运行正常;映射Value并保留Name

var a = new Test1Child {Value = 123};
var b = new Test2Child {Name = "fred", Value = 456};
Mapper.Map(a, b);
Assert.AreEqual(b.Value, 123);    // Ok
Assert.AreEqual(b.Name, "fred");  // Ok

当映射用于嵌套集合(List<Test1Child>List<Test2Child>)时,
Value已正确映射... Name的原始值已丢失!

var c = new Test1 { Children = new List<Test1Child> { new Test1Child { Value = 123 } } };
var d = new Test2 { Children = new List<Test2Child> { new Test2Child { Name = "fred", Value = 456 } } };
Mapper.Map(c, d);
Assert.AreEqual(d.Children[0].Value, 123);    // Ok
Assert.AreEqual(d.Children[0].Name, "fred");  // FAILS! Name is null.

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

正如@ MightyMuke的回答中提到的那样,@ PatrickSteele提出了一个很好的观点here:也许有意义地将每个项目从源列表自动映射到目的地list .... ie“But what if one list has 3 and the other list has 5?

就我而言,我知道source和dest列表总是具有相同的长度,并且(重要的是)源列表中的第N个项始终是dest列表中第N个项的直接对应项。

所以,这有效,但我对自己感觉不好......

Mapper.CreateMap<Test1, Test2>()
    .ForMember(m => m.Children, o => o.Ignore())
    .AfterMap((src, dest) =>
        {
            for (var i = 0; i < dest.Children.Count; i++)
                Mapper.Map(src.Children[i], dest.Children[i]);
        });

答案 1 :(得分:0)

尝试使用此问题的答案中所述的UseDestinationValueAutomapper overwrites missing source property on list with child objects

答案 2 :(得分:0)

我遇到了同样的问题。基本上,automapper不知道列表对象中的键是什么,以便将它们与原始键匹配,因此它将新建一个对象。如果您希望保留属性,则需要帮助它了解如何匹配回原始项目,以便您只需映射更改。为了做到这一点,你需要一把你现在没有的钥匙。

尝试以下内容:

public class Test1
{
    public List<Test1Child> Children { get; set; }
}
public class Test2
{
    public List<Test2Child> Children { get; set; }
}
public class Test1Child
{
    public int ChildId { get; set; }
    public int Value { get; set; }
}
public class Test2Child
{
    public int ChildId { get; set; }
    public string Name { get; set; }
    public int Value { get; set; }

    public Test2Child() 
    { }

    public Test2Child(int childId)
    {
        // of course you will need to load this from your data source, but for testing.  :)
        if (childId == 1)
        {
            ChildId = 1;
            Name = "fred";
            Value = 456;
        }
    }
}

Mapper.CreateMap<Test1, Test2>();
Mapper.CreateMap<Test1Child, Test2Child>()
    .ConstructUsing(t => t.ChildId > 0 ? new Child(t.ChildId) : new Child())
    .ForMember(m => m.Name, o => o.Ignore());

Mapper.AssertConfigurationIsValid();

var c = new Test1 { Children = new List<Test1Child> { new Test1Child { ChildId = 1, Value = 123 } } };
var d = new Test2 { Children = new List<Test2Child> { new Test2Child { ChildId = 1, Name = "fred", Value = 456 } } };
Mapper.Map(c, d);
Assert.AreEqual(d.Children[0].Value, 123);
Assert.AreEqual(d.Children[0].Name, "fred");