[英]Automapper projection doesn't include deeply nested hierarchical objects
我正在嘗試使用AutoMapper v6.1.1來使用投影映射類,但是AutoMapper不包含深層嵌套的對象。
我在此處附加了完整的Visual Studio 2015解決方案和單元測試: https : //www.dropbox.com/s/omue5ou5dvxsa57/UnitTestProject2.zip?dl=0
基本上,我試圖將映射Child
和Parent
層次到Person
的層次結構,但grand- Parents
沒有得到包含在投影的結果。
楷模:
public class Child
{
public string Name { get; set; }
public virtual Parent Parent { get; set; }
}
public class Parent
{
public string Name { get; set; }
public virtual Parent GrandParent { get; set; }
}
public class Person
{
public string Name { get; set; }
public virtual Person Parent { get; set; }
}
映射配置文件:
public class PersonProfile : Profile
{
public PersonProfile()
{
this.CreateMap<Child, Person>()
.MaxDepth(5);
this.CreateMap<Parent, Person>()
.ForMember(destinationMember => destinationMember.Parent, memberOptions => memberOptions.MapFrom(sourceMember => sourceMember.GrandParent))
.MaxDepth(5);
}
}
單元測試:
[TestClass]
public class UnitTest1
{
IMapper mapper;
List<Child> children;
[TestInitialize]
public void TestInitialize()
{
MapperConfiguration configuration = new MapperConfiguration((config =>
{
config.AddProfile(new PersonProfile());
config.ForAllMaps((mapType, mapperExpression) =>
{
mapperExpression.MaxDepth(5);
});
}));
this.mapper = configuration.CreateMapper();
mapper.ConfigurationProvider.AssertConfigurationIsValid();
this.children = new List<Child>
{
new Child
{
Name = "Child1",
Parent = new Parent
{
Name = "Parent1",
GrandParent = new Parent
{
Name = "GrandParent1",
GrandParent = new Parent
{
Name = "GreatGrandParent1"
}
}
}
}
};
}
[TestMethod]
public void TestProjection()
{
IQueryable<Person> people = children.AsQueryable().ProjectTo<Person>(mapper.ConfigurationProvider);
AssertPeople(people);
}
[TestMethod]
public void TestMap()
{
List<Person> people = mapper.Map<List<Child>, List<Person>>(children);
AssertPeople(people.AsQueryable());
}
private void AssertPeople(IQueryable<Person> people)
{
Assert.IsNotNull(people);
Assert.AreEqual(1, people.Count());
Person child1 = people.ElementAt(0);
Assert.AreEqual("Child1", child1.Name);
Person parent1 = child1.Parent;
Assert.IsNotNull(parent1);
Assert.AreEqual("Parent1", parent1.Name);
Person grandParent1 = parent1.Parent;
Assert.IsNotNull(grandParent1); // fails when using ProjectTo
Assert.AreEqual("GrandParent1", grandParent1.Name);
}
}
使用Map
方法有效,但ProjectTo
無效。
示例代碼中的類比生產中使用的類簡單得多。
我試圖用投影,這樣我可以返回一個IQueryable<Person>
從OData的,走的優勢SQL
所產生的LINQ to Entities
自動應用的查詢選項。
任何幫助表示贊賞。
謝謝!
我認為這可以描述問題: https : //github.com/AutoMapper/AutoMapper/issues/2171
但是,作為一種解決方法,不可能創建一個在內部基本上調用Map的擴展方法:
public static class Extenstions
{
public static IQueryable<TDestination> ProjectToExt<TDestination, TSource>(this IQueryable<TSource> @this,
IMapper mapper)
{
return mapper.Map<IEnumerable<TDestination>>(@this).AsQueryable();
}
}
那么調用代碼就像:
IQueryable<Person> people = children.AsQueryable().ProjectToExt<Person, Child>(mapper);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.