I ma using;
AutoMapper.Extensions.Microsoft.DependencyInjection 6.0.0
in a web api project trunning net core 2.2
when mapping my DTO object I use Automapper to map a few fields;
public class AutoMapperProfile : AutoMapper.Profile
{
public AutoMapperProfile()
{
CreateMap<ReviewPostInputModel, Review>()
.ForMember(x => x.ReceiveThirdPartyUpdates, opt => opt.MapFrom(src => src.ReceiveThirdPartyUpdates ? (DateTime?)DateTime.UtcNow : null))
.ForMember(x => x.ReceiveUpdates, opt => opt.MapFrom(src => src.ReceiveUpdates ? (DateTime?)DateTime.UtcNow : null))
.ForMember(x => x.AverageScore, opt => opt.MapFrom(src => (decimal)Math.Round((src.Courtsey + src.Reliability + src.Tidiness + src.Workmanship) / 4, 2)));
// ...
}
}
Where;
using System;
using System.Collections.Generic;
using System.Text;
public class Review
{
// ...
public decimal Reliability { get; set; }
public decimal Tidiness { get; set; }
public decimal Courtsey { get; set; }
public decimal Workmanship { get; set; }
public decimal AverageScore { get; set; }
public DateTime? ReceiveUpdates { get; set; }
public DateTime? ReceiveThirdPartyUpdates { get; set; }
}
However, when I try to map using;
var review = _mapper.Map<Review>(model);
All standard members are mapped bar my ForMember
listed above, where the DateTimes are set to a new instance of DateTime
and Averagescore is set to 0.
For completness I DI the mapper into my controller as follows;
private readonly IMapper _mapper;
public ReviewController( IMapper mapper)
{
_mapper = mapper;
}
I configure Automapper in my StartUp.cs
as follows;
services.AddAutoMapper();
I have also tried adding a test to the controller to confirm that the values from the input are not the issue (completed after the map and can confirm that this value is correctly updated);
review.AverageScore = (decimal)Math.Round((model.Courtsey + model.Reliability + model.Tidiness + model.Workmanship) / 4, 2);
Does anyone have any ideas why this is occurring?