简体   繁体   English

使用自动映射器的错误映射类型

[英]Error Mapping Types using automapper

I'm new to automapper and I'm trying very simple map.我是 automapper 的新手,我正在尝试非常简单的地图。 I have the following class and I', trying to map personmodel to person.我有以下课程,我试图将人物模型映射到人物。 It keeps failing at orders.它一直在订单失败。 Sorry if this silly but I could not figure out.对不起,如果这很愚蠢,但我无法弄清楚。 I removed all properties on orders to see what is wrong我删除了订单上的所有属性,看看有什么问题

public class Person
{
    public Person()
    {
        Orders = new List<Orders>();
    }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Id { get; set; }


    public List<Orders> Orders { get; set; }
}

public class Orders
{
    public string OrderName { get; set; }

}

public class PersonModel
{
    public PersonModel()
    {
        Orders = new List<OrderModel>();
    }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Id { get; set; }
    public List<OrderModel> Orders { get; set; }
}

public class OrderModel
{
      public string OrderName { get; set; }

}

I have the mapper defined as Mapper.Initialize(x => x.CreateMap<PersonModel, Person>());
The error that I get is:
Error mapping types.

Mapping types:
PersonModel -> Person
PersonModel -> Person

Type Map configuration:
PersonModel -> Person
PersonModel -> Person

Property:
Orders

在 Orders 和 OrderModel 之间创建映射。

Mapper.CreateMap<Orders, OrderModel>().ReverseMap();

I use this code for map a list<>我使用此代码映射列表<>

var result = NewMapperHelper.MapList<OrderModel, Orders>(YourSource);

and:和:

 public static class NewMapperHelper
  {
        public static List<TDestination> MapList<TDestination, TSource> 
             (List<TSource> entity)
            {            
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap<TSource, TDestination>().ReverseMap();
                });
    
                var _mapper = config.CreateMapper();
                return _mapper.Map<List<TDestination>>(entity);
            }
    }

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

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