自动映射投影不包括深层嵌套的分层对象

时间:2017-07-21 13:14:24

标签: c# linq automapper

我正在尝试使用AutoMapper v6.1.1使用投影来映射类,但AutoMapper不包含深层嵌套的对象。

我在这里添加了一个完整的Visual Studio 2015解决方案和单元测试: https://www.dropbox.com/s/omue5ou5dvxsa57/UnitTestProject2.zip?dl=0

我基本上是尝试将ChildParent层次结构映射到Person层次结构,但是 - 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没有。

示例代码中的类比生产中使用的类简单得多。

我正在尝试使用投影,以便我可以从OData返回IQueryable<Person>并利用SQL生成的LINQ to Entities并自动应用查询选项。

感谢任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这描述了这个问题: 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);
相关问题