我目前有以下型号:
public partial class Observation
{
public Observation()
{
Answers = new HashSet<Answers>();
ObservedEmployee = new HashSet<ObservedEmployee>();
}
public int ObservationId { get; set; }
public int WorkgroupId { get; set; }
public int LocationId { get; set; }
public int ObservationTypeId { get; set; }
public string WorkstationCode { get; set; }
public string ObservationNotes { get; set; }
public DateTime CreatedDatetime { get; set; }
public int CreatedById { get; set; }
public int? UpdatedById { get; set; }
public DateTime? UpdatedDatetime { get; set; }
public bool IsDeleted { get; set; }
public virtual Location Location { get; set; }
public virtual ObservationType ObservationType { get; set; }
public virtual Workgroup Workgroup { get; set; }
public virtual Workstation WorkstationCodeNavigation { get; set; }
public virtual ICollection<Answers> Answers { get; set; }
public virtual ICollection<ObservedEmployee> ObservedEmployee { get; set;
}
}
public partial class Answers
{
public int AnswerId { get; set; }
public int ObservationId { get; set; }
public int QuestionId { get; set; }
public int OptionId { get; set; }
public int? SubOptionId { get; set; }
public virtual Observation Observation { get; set; }
public virtual OptionCatalog Option { get; set; }
public virtual QuestionCatalog Question { get; set; }
public virtual SubOptionCatalog SubOption { get; set; }
}
以及相应的DTO
public class ObservationDTO
{
public int ObservationId { get; set; }
public int WorkgroupId { get; set; }
public int LocationId { get; set; }
public int ObservationTypeId { get; set; }
public string WorkstationCode { get; set; }
public string ObservationNotes { get; set; }
public DateTime CreatedDatetime { get; set; }
public int CreatedById { get; set; }
public int? UpdatedById { get; set; }
public DateTime? UpdatedDatetime { get; set; }
public bool IsDeleted { get; set; }
public ICollection<AnswersDTO> Answers { get; set; }
public ICollection<ObservedEmployeeDTO> ObservedEmployee { get; set; }
public WorkgroupDTO Workgroup { get; set; }
}
public class AnswersDTO
{
public int AnswerId { get; set; }
public int ObservationId { get; set; }
public int QuestionId { get; set; }
public int OptionId { get; set; }
public int? SubOptionId { get; set; }
public string QuestionText { get; set; }
public string OptionText { get; set; }
public string SubOptionText { get; set; }
}
当我将观察值映射到ObservationDTO时,我希望ICollection是ICollection。考虑到这一点,我进行了以下映射
cfg.CreateMap<Answers, AnswersDTO>()
.ForMember(dest => dest.QuestionText, opt => opt.MapFrom(src => src.Question.QuestionText))
.ForMember(dest => dest.OptionText, opt => opt.MapFrom(src => src.Option.OptionText))
.ForMember(dest => dest.SubOptionText, opt => opt.MapFrom(src => src.SubOption.SubOptionText));
cfg.CreateMap<Observation, ObservationDTO>()
.ReverseMap();
在这里我称为映射,下面是服务调用的代码
public IEnumerable<ObservationDTO> GetUserObservations(int id)
{
var observations = _observations.GetUserObservations(id);
return _mapper.Map<IEnumerable<ObservationDTO>>(observations);
}
public IEnumerable<Observation> GetUserObservations(int id)
{
DateTime date = DateTime.Now.AddYears(-1);
date = new DateTime(date.Year, date.Month, 1, 0, 0, 0);
return _context.Observation
.Where(obs => obs.CreatedById == id && obs.CreatedDatetime >= date)
.Include(i => i.Answers)
.Include(i => i.Workgroup)
.ToList();
}
这是我从后端得到的响应
{
"observationId": 11,
"workgroupId": 1,
"locationId": 1,
"observationTypeId": 1,
"workstationCode": "DFW",
"observationNotes": "I would enter notes here if I had any.",
"createdDatetime": "2019-01-30T17:19:27",
"createdById": 30633,
"updatedById": null,
"updatedDatetime": null,
"isDeleted": false,
"answers": [
{
"answerId": 1,
"observationId": 11,
"questionId": 3,
"optionId": 1,
"subOptionId": 2,
"questionText": null,
"optionText": null,
"subOptionText": null
}
]
}
我的问题是,当前,当我将此DTO退还给我时,所有ForMember添加项都为空。这些条目的文本确实具有值,所以我知道它无法正常工作。这使我假设Collection没有被映射。我对这个工具还很陌生,但是我对这个问题(包括文档)进行了至少4-5个小时的研究,我有些迷茫。我不确定是否错过了必要的包含(我已经尝试过所有我能想到的)或我的映射配置不正确。如果有人对这个问题有任何想法或指导,将不胜感激。