2010-03-15 48 views
2

我有以下類:AutoMapper問題 - 列表不會地圖

public class Account 
{ 
    public int AccountID { get; set; } 
    public Enterprise Enterprise { get; set; } 
    public List<User> UserList { get; set; } 
} 

和我有以下方法段:

Entities.Account accountDto = new Entities.Account(); 
DAL.Entities.Account account; 

Mapper.CreateMap<DAL.Entities.Account, Entities.Account>(); 
Mapper.CreateMap<DAL.Entities.User, Entities.User>(); 

account = DAL.Account.GetByPrimaryKey(this.Database, primaryKey, withChildren); 

Mapper.Map(account,accountDto); 
return accountDto; 

當方法被調用時,賬戶類被映射正確,但Account類中的用戶列表不(它是NULL)。 List中有四個用戶實體應該被映射。有人能告訴我什麼可能是錯的嗎?

回答

3

嘗試不傳入accountDto,並讓AutoMapper爲您創建它。當您映射到現有的目標對象時,AutoMapper會做出一些假設,您將不會有任何一個已爲空的目標集合。相反,這樣做:

var accountDto = Mapper.Map<DAL.Entities.Account, Entities.Account>(account); 

你應該檢查的最後一件事是,你的配置是有效的,所以你可以嘗試:

Mapper.AssertConfigurationIsValid(); 

那些CreateMap後來電。這將檢查以確保事物的目標類型一切正確排列。