В настоящее время у меня есть следующие модели:
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; }
}
Когда я сопоставляю Observation 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();
}
А вот ответ Iполучить из бэкэнда
{
"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 равны нулю.Эти записи имеют значения для своего текста, поэтому я знаю, что он работает неправильно.Это заставляет меня предположить, что Коллекция не отображается.Я довольно новичок в этом инструменте, но я занимался этим вопросом как минимум 4-5 часов, включая документацию, и я немного растерялся.Я не уверен, пропустил ли я необходимое включение (я перепробовал все, что мог придумать) или моя конфигурация отображения неверна.Если у кого-то есть какие-либо мысли или рекомендации по этому вопросу, это будет с благодарностью.