AutoMapper : Error when mapping between two lists

41 views Asked by At

Source/destination types

public partial class MailMessage
{
    public List<MailMessageRecipient> MailMessageRecipients { get; set; }
}

public class CreateDraftInputDto : EntityDto<Guid>
{
    [Required]
    public List<CreateDraftInput_ToRecipientDto> MailMessageRecipients { get; set; }
}

public class CreateDraftInput_ToRecipientDto : EntityDto<Guid>
    {
        public bool ToRecipient { get; set; }
        public bool CcRecipient { get; set; }
        public bool CciRecipient { get; set; }
    }

public partial class MailMessageRecipient : Entity.Entity, ITenantMailAddress
{
    public Guid MailAddressId { get; set; }

    public Guid MailMessageId { get; set; }

    public bool ToRecipient { get; set; }
    public bool CcRecipient { get; set; }
    public bool CciRecipient { get; set; }
}

Mapping configuration

CreateMap<CreateDraftInput_ToRecipientDto, MailMessageRecipient>(MemberList.None);

CreateMap<CreateDraftInputDto, MailMessage>(MemberList.None);

Version: 12.0.0.0

Expected behavior

Before mapping, I get the MailMessage from my repository including the MailMessageRecipients, they are having already their MailAddressId and MailMessageId set. I expect those fields to remaining themselves as they are not in my DTO.

Actual behavior

These fields are set to Guid.Empty. I have tried many tests and some work : if I declare my MailMessageRecipients inside MailMessage as an ICollection<MailMessageRecipient>, and then do this : ObjectMapper.Map(input.MailMessageRecipients, mailMessage.MailMessageRecipients.ToList()); It works, as well as converting this ICollection toArray(). But when it's declared as a List inside my Entity, it doesn't work anymore.

Thing is I can't always convert ToList() this way, because I need to do the mapping on the parent which is more like ObjectMapper.Map(CreateDraftInputDtoInstance, MailMessageInstance);

Steps to reproduce

MailMessage mailMessage = await _mailMessageRepository.GetMailMessage(input.Id).ConfigureAwait(false);
ObjectMapper.Map(input.MailMessageRecipients, mailMessage.MailMessageRecipients);
0

There are 0 answers