2011-05-09 185 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