Reputation: 6467
I am using latest AutoMapper.Extensions.Microsoft.DependencyInjection 6.1.0. I have two classes
public class ConversionRate
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[Required]
public string FromCurrency { get; set; }
[Required]
public string ToCurrency { get; set; }
[Required]
[Range(0, 100000)]
public double Value { get; set; }
[Required]
public DateTime Date { get; set; }
[ForeignKey("ProviderId")]
public Provider Provider { get; set; }
public int ProviderId { get; set; }
}
and
public class RateDto
{
public DateTime Date { get; set; }
public double Value { get; set; }
}
this is the automapper profile
public class ConversionRateProfile : Profile
{
public ConversionRateProfile()
{
CreateMap<ConversionRate, RateDto>();
CreateMap<RateDto, ConversionRate>();
}
}
and I get an error that there are unmapped properties
Unmapped properties:
Id
FromCurrency
ToCurrency
Provider
ProviderId
I was under the impression that automapper simply ignores the properties that exist in destination but not in source. What is the problem here?
Upvotes: 0
Views: 999
Reputation: 46501
You can specify a MemberList
enum value to the CreateMap
method which configures whether to validate the source, destination, or none of the properties of the type are validated after mapping. In your case you should specify MemberList.Source
from the RateDto
to the ConverionRate
types so only the mapping of properties on the source type (RateDto
) are validated. The mapping from ConversionRate
to RateDto
should use MemberList.Destination
to make sure all the properties in RateDto
are mapped from ConversionRate
. MemberList.Destination
is the default value, so you don't have to explicitly specify it in the mapping profile.
You can configure the configuration validation in your mapping profile like this:
public ConversionRateProfile()
{
CreateMap<ConversionRate, RateDto>();
CreateMap<RateDto, ConversionRate>(MemberList.Source);
}
Upvotes: 4