automapper lost value when source contain navigation property

Versions:

ASP.NET Core 3.1 AutoMapper 10.0.0 EF Core 3.1.6 

My entities:

public class Room {     [Key]     public int Id { get; set; }      // not equal to 'RoomDetail.StdSectionId',      // this is custom section id, ex. MyFrance     public int SectionId { get; set; }     public virtual Section Section { get; set; }      public string DisplayName { get; set; } }  public class RoomDetail {     [Key]     public int Id { get; set; }      public int RoomId { get; set; }     public Room Room { get; set; }      // not equal to 'Room.SectionId',      // this is origin section, ex: France     [ForeignKey("Section")]     public int StdSectionId { get; set; }       public virtual Section Section { get; set; }      public int Floor { get; set; } } 

My DTO:

public class RoomDetailDTO {     public int RoomId { get; set; }      public int Floor { get; set; }      public int SectionId { get; set; }      public int StdSectionId { get; set; } } 

I have the following code:

// Mapping configuration CreateMap<Room, RoomDetailDTO>(); CreateMap<RoomDetail, RoomDetailDTO>();  // Controller var query = (from room in _context.Room              join roomDetail in _context.RoomDetail on room.Id equals roomDetail.RoomId             where room.Id.Equals(4)             select new { room, roomDetail }).AsNoTracking().AsEnumerable();  var roomInfo = query.First(); // roomInfo =  // { //     "room": { //         "id": 4, //         "sectionId": 15, //         "section": null, //         "displayName": "MyRoom" //     }, //     "roomDetail": { //         "id": 9, //         "roomId": 4, //         "room": null, //         "stdSectionId": 1, //         "section": null, //         "floor": 2 //     }, // } var dto = _mapper.Map<RoomDetailDTO>(roomInfo.room); _mapper.Map(roomInfo.roomDetail, dto); 

Expected:

dto = {     "roomId": 4,     "floor": 2,     "sectionId": 15,     "stdSectionId": 1, } 

But actual behavior:

dto = {     "roomId": 4,     "floor": 2,     "sectionId": 0,     "stdSectionId": 1, } 

The problem is that RoomDetailDTO.SectionId is zero after the mapping. I tried to remove the navigation property Section from RoomDetail. It work. The RoomDetailDTO.SectionId is mapped. But I sometimes need to use the navigation property, so I can’t to remove it.

And I tried to ignore the navigation property, but not working :

public static IMappingExpression<TSource, TDestination> IgnoreAllVirtual<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression) {     var desType = typeof(TDestination);     foreach (var property in desType.GetProperties().Where(p => p.GetGetMethod().IsVirtual))     {         expression.ForMember(property.Name, opt => opt.Ignore());     }      var srcType = typeof(TSource);     foreach (var property in srcType.GetProperties().Where(p => p.GetGetMethod().IsVirtual))     {         expression.ForSourceMember(property.Name, opt => opt.DoNotValidate());     }      return expression; } 

I don’t really understand what the AutoMapper code is doing so it’s hard to debug. If someone could explain mapper how to works for navigation property that would be great.

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.