C# 自动映射子列表属性

C# 自动映射子列表属性,c#,automapper,C#,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;}

我正在尝试将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 int CategoryId{get;set;}
公共列表产品{get;set;}
}
公共类产品
{
public int ProductId{get;set;}
public int CategoryId{get;set;}
公共类别{get;set;}
}
视图模型

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

public class ProductVM
{
    public int ProductId {get; set;}
}
公共类类别
{
public int CategoryId{get;set;}
公共列表产品{get;set;}
}
公共类ProductVM
{
public int ProductId{get;set;}
}
然后此自动映射代码:

Mapper.CreateMap<CategoryVM, Category>();
Category category = Mapper.Map<CategoryVM, Category>(_category);
Mapper.CreateMap();
Category Category=Mapper.Map(_Category);
它在
产品
属性上抛出错误:

正在尝试将WebUI.ViewModel.ProductVM映射到Domain.Product。
使用WebUI.ViewModel.ProductVM到域的映射配置。产品目标属性:
产品缺少类型映射配置或映射不受支持。
引发了类型为“AutoMapper.AutoMappingException”的异常。

我猜我把子属性映射错了还是怎么了?如果您有任何见解,我们将不胜感激。

您需要创建

类似的问题:

您需要创建


类似的问题:

您还需要从ProductVM到ProductVM的映射


自动映射正在查找匹配的属性,但不知道如何映射它们。

您还需要从ProductVM到ProductVM的映射


自动映射发现这些属性匹配,但不知道如何映射它们。

Man…如果自动映射无法自动执行此操作,那又有什么意义呢?自动映射从未设计用于从VM=>域到VM,最好用于从一个域到另一个VM。Man…如果自动映射无法自动执行此操作,这有什么意义?Automapper从来都不是为从VM=>域转到VM而设计的,最好是用于从一个域转到另一个VM。感谢jfar,你挽救了这一天。感谢jfar,你挽救了这一天。可能重复的可能重复的