ef code first - AutoMpper + Map复杂嵌套的多对多关系



我有这样的域模型

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,但我不知道如何做到这一点…任何建议或帮助

我不知道你想要达到什么目的。

我知道你想映射到EntityDTO,但是从其他什么类型呢?我将假设您想使用EntityTwo作为源。

在这种情况下,

  • EntityOnePropertyOne:将通过从源(EntityTwo)平坦自动获得-所以,这里没有问题。
  • EntityThreePropertyOne_ValueOne:这将假设您有一个名为EntityThree的属性(您有),并且在该类型中,有一个名为PropertyOne_ValueOne的类型为int(您没有)。

另一种方法会变得更棘手,因为我看到会有很多属性被忽略,所以你需要告诉AutoMapper,你不希望它关心你的复杂类型中的所有属性,这些属性不是来自DTO

最新更新