При маппинге класса через AutoMapper на самого себя, объект вместо обновления удаляется. В чем может быть проблема?
Есть сервис для обновления сущности, но вместо обновления сущность удаляется
public async Task<Result<Guid>> UpdateRiskIndicatorAsync(RiskIndicatorDto indicatorDto)
{
var indicator = await _context.RiskIndicators.FindAsync(indicatorDto.Id);
if (indicator == null) return null;
var newIndicator = _mapper.Map<RiskIndicator>(indicatorDto);
var validation = IsCorrectIndicatorRelationship(newIndicator);
if (!validation.IsSuccess) return validation;
_mapper.Map(newIndicator, indicator);
_context.RiskIndicators.Update(indicator);
var result = await _context.SaveChangesAsync() > 0;
if (!result) return Result<Guid>.Failure("Failed to update the indicator");
return Result<Guid>.Success(indicator.Id);
}
Класс профайла выглядит следующим образом
public class RiskIndicatorDtoProfile : Profile
{
public RiskIndicatorDtoProfile()
{
CreateMap<RiskIndicator, RiskIndicatorDto>().ReverseMap();
CreateMap<CreateRiskIndicatorDto, RiskIndicator>();
CreateMap<RiskIndicator, RiskIndicatorDtoForWork>();
CreateMap<RiskIndicator, RiskIndicator>()
.ForMember(d => d.CreatedAt, s => s.Ignore())
.ForMember(d => d.CreatedEmployeeId, s => s.Ignore())
.ForMember(dest => dest.UpdatedAt, opt => opt.MapFrom(src => DateTime.Now));
}
}
//валидация связи между дочерней и родительской задачей
private Result<Guid> IsCorrectIndicatorRelationship(RiskIndicator child)
{
if(child.ParentIndicatorId != null)
{
var parent = _context.RiskIndicators.Find(child.ParentIndicatorId);
if (parent == null)
return Result<Guid>.Failure("There is no such task by ParentIndicatorId, the task has not been created");
if(parent.StartDate > child.StartDate || parent.EndDate < child.EndDate)
return Result<Guid>.Failure("The date of the child indicator cannot go beyond the parent");
}
if (child.RiskId != null)
{
var risk = _context.Risks.Find(child.RiskId);
if(risk == null)
return Result<Guid>.Failure($"The risk with {child.RiskId} id was not found");
}
return Result<Guid>.Success(Guid.Empty);
}