自动映射:具有相同名称而不映射的类和属性

时间:2011-12-15 17:17:34

标签: c# automapper

下面是我正在描述的准系统类设置。

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.recipientProvider,它可以正常工作。所以它让我相信它的类名和属性是同一个导致问题的事实。有关如何修复它或是否有错误的任何见解?

1 个答案:

答案 0 :(得分:0)

尝试使用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;}
}

希望它有所帮助。