自动映射以映射子列表属性

时间:2011-05-09 17:48:58

标签: automapper

我试图将viewmodel映射到如下所示的域:

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.

我猜我正在映射子属性错误或什么?任何见解都将不胜感激。

2 个答案:

答案 0 :(得分:2)

您还需要从ProductVM到产品的地图

自动映射是找到这些属性匹配但不知道如何映射它们。

答案 1 :(得分:1)