Do i need to create automapper createmap both ways?

后端 未结 4 1404
情深已故
情深已故 2021-02-05 00:08

This might be a stupid question! (n00b to AutoMapper and time-short!)

I want to use AutoMapper to map from EF4 entities to ViewModel classes.

1) If I call

<
4条回答
  •  日久生厌
    2021-02-05 00:48

    1. Yes, or you can call CreateMap().ReverseMap().
    2. If two classes have same Member(Property,Field,GetMethod()), you needn't call CreateMap. Actually, if every member in TDest are all exist in TSrc, you needn't call CreateMap. The following code works.
    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }  
    }
    class Person2
    {
       public string Name { get; set; }
       public int? Age { get; set; }
       public DateTime BirthTime { get; set; }
    }
    public class NormalProfile : Profile
    {
        public NormalProfile()
        {
           //CreateMap();//
        }
    }
       
    var cfg = new MapperConfiguration(c => 
    { 
        c.AddProfile();
    });
    //cfg.AssertConfigurationIsValid();
    var mapper = cfg.CreateMapper();
    var s3 = mapper.Map(new Person2 { Name = "Person2" });
    

提交回复
热议问题