AutoMapper + Map Complex嵌套多对多关系

时间:2014-03-26 20:46:04

标签: ef-code-first automapper automapper-3

我有像这样的域名模型

public class EntityOne
    {
        public int EnityOneId { get; set; }
        public int EntityOnePropertyOne { get; set; }

        public List<EntityTwo> EntityTwos { get; set; }
    }

    public class EntityTwo
    {
        public int EntityTwoId { get; set; }
        public string EntityTwoPropertyOne { get; set; }

        public int EntityThreeId { get; set; }
        public int EnityOneId { get; set; }

        public virtual EntityOne EntityOne { get; set; }
        public virtual EntityThree EntityThree { get; set; }
    }

    public class EntityThree
    {
        public int EntityThreeId { get; set; }
        public string EntityThreePropertyOne { get; set; }
    }

我有像这样的DTO

public class EntityDTO
    {
        public int EntityOnePropertyOne { get; set; }
        public string EntityThreePropertyOne_ValueOne { get; set; }
        public string EntityThreePropertyOne_ValueTwo { get; set; }
        public string EntityThreePropertyOne_ValueThree { get; set; }
        public string EntityThreePropertyOne_ValueFour { get; set; }
        public string EntityThreePropertyOne_ValueFive { get; set; }
    }

我想配置从DTO到DomainModel的映射,反之亦然使用AutoMapper,但我不知道如何做...任何建议或帮助

1 个答案:

答案 0 :(得分:0)

我不确定你在这里要做什么。

我想要映射到EntityDTO,但是从其他类型?我假设您要使用EntityTwo作为来源。

在那种情况下,

  • EntityOnePropertyOne:将通过源文件中的展平自动获取(EntityTwo) - 所以,这里没问题。
  • EntityThreePropertyOne_ValueOne:这将假设您有一个名为EntityThree的属性(您可以这样做),并且在该类型中,还有一个名为PropertyOne_ValueOne的{​​{1}}属性(其中)你不是。同样适用于其余部分。

另一种方式会变得更加棘手,因为我发现会忽略很多属性,所以你需要告诉AutoMapper,你不希望它关注复杂的所有属性。类型,不是来自DTO。

相关问题