使用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.
我该如何解决这个问题?
答案 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)
尝试使用此问题的答案中所述的UseDestinationValue
:
Automapper 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");