簡體   English   中英

自動映射器:具有相同名稱的類和屬性未映射

[英]Automapper: Class and property with identical names not mapping

以下是我所描述的准系統類設置。

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,它可以正常工作。 因此,這使我相信類名稱和屬性相同的事實導致了問題。 關於如何修復或是否存在錯誤的任何見解?

嘗試使用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;}
}

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM