2011-05-09 187 views
0

我試着去一個視圖模型映射到類似如下的域:Automapper映射子列表屬性

public class Category 
{ 
    public int CategoryId {get; set;} 
    public List<Product> Products {get; set;} 
} 

public class Product 
{ 

    public int ProductId {get; set;} 
    public int CategoryId {get; set;} 
    public Category Category {get; set;} 
} 

視圖模型

public class CategoryVM 
{ 
    public int CategoryId {get; set;} 
    public List<ProductVM> Products {get; set;} 
} 

public class ProductVM 
{ 
    public int ProductId {get; set;} 
} 

那麼這自動映射代碼:

Mapper.CreateMap<CategoryVM, Category>(); 
Category category = Mapper.Map<CategoryVM, Category>(_category); 

它輕視的Products屬性的錯誤:

Trying to map WebUI.ViewModel.ProductVM to Domain.Product. Using mapping configuration for WebUI.ViewModel.ProductVM to Domain.Product Destination property: Products Missing type map configuration or unsupported mapping. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.

即時猜測我映射子屬性錯了或什麼?任何洞察力將不勝感激。

+0

可能重複[Problem auto mapping =>視圖模型集合而不是另一個視圖模型](http://stackoverflow.com/questions/5691270/problem-auto-mapping-collection-of-view-models-instead-另一個視圖模型) – 2012-11-12 21:26:43

回答

2

您還需要一張地圖從ProductVM產品

自動映射是找到這些屬性相匹配,但不知道如何映射他們和他們。

+0

我編輯了這個問題 – 2011-05-09 19:55:31

+0

謝謝jfar,你保存了一天。 – Sam 2012-09-12 16:53:36