2012-06-17 99 views
1

我正在使用Automapper來映射我的域模型和DTO。 當我映射Mapper.Map<SiteDTO, SiteEntity>它工作正常自動映射集合

但是,當我使用相同的實體的集合,它不映射。

Mapper.Map<Collection<SiteEntity>, Collection<SiteDTO>>(siteEntityCollection); 

按Automapper Wiki,它說,實施ICollection名單將被映射,我使用集合實現ICollection的,但automapper不映射。難道我做錯了什麼。

public class SiteEntity //SiteDTO has exactly the same properties, so I am not posting it here. 
    { 
     public int SiteID { get; set; } 
     public string Code { get; set; } 
     public string Name { get; set; } 
     public byte Status { get; set; } 
     public int ModifiedBy { get; set; } 
     public DateTime ModifiedDate{ get; set; } 
     public long TimeStamp{ get; set; } 
     public string Description{ get; set; } 
     public string Notes{ get; set; } 
     public ObservableCollection<AreaEntity> Areas{ get; set; } 
     public void SiteEntity() 
     { 
      Areas=new ObservableCollection<AreaEntity>(); 
     } 
    } 

編輯: SiteEntity更新,包括構造函數。

+1

您是否在映射器中設置了內部集合的映射?你有映射爲AreaEntity AreEntityDto設置? –

+0

@EIYusubov感謝您的回答,但我無法解決問題。我會發布我今天晚些時候用於映射的實體和代碼。也許如果可能的話,你可以看一看,並提出發生了什麼問題。 –

回答

1

我一直在使用IList<>沒有任何問題。 我會先檢查子域模型的映射。 很可能他們還沒有設置。在你的例子中:映射AreaEntity - > AreaEntityDto。從維基

Mapper.Map<AreaEntity, AreaEntityDto> 

代碼示例:

Mapper.CreateMap<ParentSource, ParentDestination>() 
    .Include<ChildSource, ChildDestination>(); 
Mapper.CreateMap<ChildSource, ChildDestination>(); 
0

根據您發佈Automapper將無法映射,因爲你沒有爲SiteEntity一個默認的構造函數創建一個新的ObservableCollection地區代碼。

因爲這不存在,所以當它試圖映​​射區域時,你會得到一個空引用異常。

+0

在原始代碼中,我有構造函數來實例化ObservableCollection區域。我只是沒有在這裏發佈它來簡化問題。請參閱更新的SiteEntity。 –