Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

c# - Mapper overwrites unchanged fields

In my .Net Core 3.1 project I map a viewmodel to a dto, and the dto subsequently to my entity. All fields are mapped in my MappingProfile.

Now I want to update them in the database, but AutoMapper keeps overwriting unchanged values with _mapper.Map(dto, entity).

MappingProfile:

    CreateMap<Playlist, PlaylistDTO>()
        .ForMember(x => x.Id, x => x.MapFrom(y => y.Id))
        .ForMember(x => x.PlayListName, x => x.MapFrom(y => y.PlayListName))
        .ForMember(x => x.Description, x => x.MapFrom(y => y.Description))
        .ForMember(x => x.Owner, x => x.MapFrom(y => y.Owner))
        .ForMember(x => x.OwnerId, x => x.MapFrom(y => y.OwnerId))
        .ForMember(x => x.Created, x => x.MapFrom(y => y.Created))
        .ForMember(x => x.CreatedBy, x => x.MapFrom(y => y.CreatedBy))
        .ForMember(x => x.LastModified, x => x.MapFrom(y => y.LastModified))
        .ForMember(x => x.LastModifiedBy, x => x.MapFrom(y => y.LastModifiedBy))
        .ForMember(x => x.PlaylistVideos, x => x.MapFrom(y => y.PlaylistVideos))
        .ReverseMap();

    CreateMap<IPlaylistEditVM, PlaylistDTO>()
        .IgnoreAllPropertiesWithAnInaccessibleSetter()
        .ForMember(x => x.Id, x => x.MapFrom(y => y.Id))
        .ForMember(x => x.PlayListName, x => x.MapFrom(y => y.PlaylistName))
        .ForMember(x => x.Description, x => x.MapFrom(y => y.Description))
        .ForMember(s => s.OwnerId, opt => opt.Ignore())
        .ForMember(s => s.Owner, opt => opt.Ignore())
        .ForMember(s => s.CreatedBy, opt => opt.Ignore())
        .ForMember(s => s.CreatedBy, opt => opt.Ignore())
        .ReverseMap();

If the viewmodel is mapped to the dto with PlaylistDTO dto = _mapper.Map<PlaylistDTO>(vm);, the dto is used to save it to the database. The final Edit method where the mapping goes wrong:

public async Task<(bool success, string msg, PlaylistDTO dto)> UpdatePlaylist(PlaylistDTO dto)
{
    Playlist originalModel = _context.Playlists.Where(pl => pl.Id.ToString() == dto.Id).FirstOrDefault();

    dto.LastModified = DateTime.Now;
    _mapper.Map(dto, originalModel);

    try
    {
        _context.Playlists.Update(originalModel);
        await _context.SaveChangesAsync(new CancellationToken());
    }
    catch (Exception ex)
    {
        return (false, ex.Message, null);
    }
    dto = _mapper.Map<PlaylistDTO>(originalModel);
    return (true, String.Empty, dto);
}

With this, my Created and my OwnerID fields are overwritten with null values. So for my edit method, I only want to map the fields of PlaylistName, Description, LastModified and LastModifiedBy. All the other fields should keep whatever value they already have in originalModel

question from:https://stackoverflow.com/questions/65862560/mapper-overwrites-unchanged-fields

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...