我试图将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.
我猜我正在映射子属性错误或什么?任何见解都将不胜感激。
答案 0 :(得分:2)
您还需要从ProductVM到产品的地图
自动映射是找到这些属性匹配但不知道如何映射它们。
答案 1 :(得分:1)