【问题标题】:How to get Automapper to map an attribute that is part of another table如何让 Automapper 映射属于另一个表的属性
【发布时间】:2019-10-20 03:11:33
【问题描述】:

我有 3 个相关的表。员工,相对,RelationTypeCatalog。关系是 IX_Relatives_EmployeeIdIX_Relatives_RelationTypeCatalogId 。我想要实现的是,在返回员工的亲属列表时,我希望“关系”字段显示存储在“RelationType”中的值,该值是 RelationTypeCatalog 类的属性。
我是编码和自动映射器的新手,我真的可以使用一些帮助和指导。我能够将“关系”字段设为空。这是一些代码

    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime DateOfBirth { get; set; }
        public string DNI { get; set; }

        public ICollection<Relative> Relatives { get; set; }

    }

    public class Relative
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime DateOfBirth { get; set; }
        public Employee Employee { get; set; }
        public int EmployeeId { get; set; }
        public RelationTypeCatalog RelationTypeCatalog { get; set; }
        public int RelationTypeCatalogId { get; set; }   
    }

    public class RelationTypeCatalog
    {

        public int Id { get; set; }
        public string RelationType { get; set; }
        public ICollection<Relative> Relatives { get; set; }

    }

    public class RelativeToReturnDto
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Relationship { get; set; }

    }

and here is the Automapper

    public class AutoMapperProfiles : Profile
    {
          CreateMap<Relative, RelativeToReturnDto>()
            .ForMember(dest => dest.Age,
            opt => opt.MapFrom(src => src.DateOfBirth.CalculateAge()))
            .ForMember(dest => dest.Relationship,
            opt => opt.MapFrom(src => src.RelationTypeCatalog.RelationType));
    }

这是我得到的 JSON 响应。

{
    "id": 1,
    "name": "Camacho",
    "age": 29,
    "dni": "(865) 494-2026",
    "relatives": [
        {
            "id": 3,
            "name": "Diego",
            "age": 15,
            "relationship": null
        }
}

我期望得到"relationship": "Espouse" 这是“RelationType”字段中的对应值

【问题讨论】:

  • 这看起来应该可以工作 - 你确定 RelationTypeCatalog 在映射之前有一个值吗?
  • 是的,Rob,我做了一个简单的测试,将“RelationTypeCatalogId”添加到“RelativeToReturnDto”字段并返回一个值,我只是无法让 automapper 弄清楚“是什么”关系类型'
  • 我看不出 RelationTypeCatalogId 对手头的问题有什么影响 - 您能否通过调试器或其他方法确认您的相对对象实际上具有非空 RelationTypeCatalog 属性在您尝试将其映射到您的 DTO 之前,RelationType 设置为 espouse。
  • 你说得对,谢谢。在 map 之前,relationType 实际上是 null

标签: c# .net-core automapper


【解决方案1】:

正如 Rob 所说,问题似乎是由您的输入引起的,而不是由 AutoMapper 引起的,如下例所示,Relationship 被映射:

var mapper = new Mapper(new MapperConfiguration(cfg => cfg.AddProfile<AutoMapperProfiles>()));
var relative = new Relative { RelationTypeCatalog = new RelationTypeCatalog { RelationType = "rel1" } };
var dto = mapper.Map<RelativeToReturnDto>(relative);
Debug.Assert(dto.Relationship == "rel1");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 2019-08-02
    • 2020-02-29
    • 1970-01-01
    相关资源
    最近更新 更多