如何让 Automapper 映射属于另一个 table 的属性
How to get Automapper to map an attribute that is part of another table
我有 3 个相关的表。员工、亲戚、RelationTypeCatalog。关系是 IX_Relatives_EmployeeId
和 IX_Relatives_RelationTypeCatalogId
。我想要实现的是,当返回员工的亲属列表时,我希望字段 'Relationship' 显示存储在 'RelationType' 中的值,即 RelationTypeCatalog 的 属性 class.
我是编码和自动映射器的新手,我真的需要一些帮助和指导。我能够将“Relationship”字段设为空。这是一些代码
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'字段
中的对应值
正如 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");
我有 3 个相关的表。员工、亲戚、RelationTypeCatalog。关系是 IX_Relatives_EmployeeId
和 IX_Relatives_RelationTypeCatalogId
。我想要实现的是,当返回员工的亲属列表时,我希望字段 'Relationship' 显示存储在 'RelationType' 中的值,即 RelationTypeCatalog 的 属性 class.
我是编码和自动映射器的新手,我真的需要一些帮助和指导。我能够将“Relationship”字段设为空。这是一些代码
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'字段
正如 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");