简体   繁体   English

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

[英]Automapper to map child list properties

Im trying to map a viewmodel to a domain that looks like the following: 我试图将一个视图模型映射到如下所示的域:

domain

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;}
}

viewModel viewModel

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

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

Then this automapper code: 然后此自动映射器代码:

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

It throws the error on the Products property: 它在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.

Im guessing I'm mapping child properties wrong or something? 我猜我在映射子属性错误或什么? Any insight would be appreciated. 任何见识将不胜感激。

You'll also need a map from ProductVM to Product 您还需要从ProductVM到产品的映射

Automapping is finding these properties match but doesn't know how to map them and forth. 自动映射发现这些属性匹配,但不知道如何来回映射它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM