Automapper:具有相同名称的类和属性,不映射

本文关键字:属性 映射 Automapper | 更新日期: 2023-09-27 18:18:03

下面是我所描述的一个基本类设置。

public class List
{
   public int Id {get;set;}
   public RecipientCount RecipientCount {get;set;}
   public RecipientCount SomeOtherName {get;set;}
}
public class RecipientCount
{
   public int Total {get;set;}
   public int Active {get;set;}
}
public class ListDto
{
   public int id {get;set;}
   public RecipientCountDto recipientCount {get;set;}
   public RecipientCountDto someOtherName {get;set;}
}
public class RecipientCountDto
{
   public int total {get;set;}
   public int active {get;set;}
}
public class AutoMapperConfiguration
{
    public void Init(AutoMapper.IConfiguration config)
    {
       config.CreateMap<RecipientCount,RecipientCountDto>();
    }
}

如果我尝试使用这个,它会抛出:

   The following property on Reachmail.Domain.Component.RecipientCountDto cannot 
   be mapped: 
   recipientCount
   Add a custom mapping expression, ignore, add a custom resolver, or modify the 
   destination type Reachmail.Domain.Component.RecipientCount.
   Context:
   Mapping to property recipientCount of type Reachmail.Domain.Component.RecipientCountDto 
   from source type Reachmail.Domain.Component.RecipientCount
   Mapping to type Reachmail.Web.UI.Controllers.ListDto from source type 
   Reachmail.Domain.Contacts.Lists.List
   Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.

然而,如果我删除RecipientProviderDto。它工作得很好。所以它让我相信,它的事实,类名和属性是相同的,导致了一个问题。对于如何修复它或它是否是一个bug有什么见解吗?

Automapper:具有相同名称的类和属性,不映射

try with UpperLetter:

public class ListDto
{
   public int Id {get;set;}
   public RecipientCountDto RecipientCount {get;set;}
   public RecipientCountDto SomeOtherName {get;set;}
}
public class RecipientCountDto
{
   public int Total {get;set;}
   public int Active {get;set;}
}

希望能有所帮助。