AutoMapper.Mapper.CreateMap()' is obsolete

前端 未结 3 360
忘了有多久
忘了有多久 2020-12-31 04:58

I have to classes Like

class A
{
 public int id {get; set;}
}

class B
{
 public C c {get; set;}
}

class C
{
 public int id {get; set;}
 public string Name          


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 05:30

    Another way that seems a bit cleaner is to make a MappingProfile class which inherits from the Profile class of AutoMapper

    public class MappingProfile:Profile
    {
        public MappingProfile()
        {
            CreateMap();
            CreateMap();
            ...
        }
    }
    

    Then you initialize the mapping with Mapper.Initialize(c => c.AddProfile()); in your startup code

    That will allow you to use the mapping anywhere by calling

    destination1Collection = source1Collection.Select(Mapper.Map);
    

提交回复
热议问题