0
votes

Hi I saw many examples for this theme but each one that I tried did not work for me. I want to map the Model from Dto on the post method and again reverse for the response.The model contains Enums but the Dto strings.Here is the problem:

Type Map configuration: HistoryTransactionDto -> HistoryTransaction TransactionHistoryService.Dtos.HistoryTransactionDto -> TransactionHistoryService.Data.Models.HistoryTransaction

Destination Member: TransactionType

---> System.ArgumentException: The value passed in must be an enum base or an underlying type for an enum, such as an Int32. (Parameter 'value')

    public class HistoryTransaction
    {
        public string Status { get; set; }  

        public TransactionType TransactionType { get; set; }

        public TransactionNetworkType TransactionNetworkType { get; set; }


    public class HistoryTransactionDto
    {
        public string Status { get; set; }

        public string TransactionType { get; set; }

        public string TransactionNetworkType { get; set; }


     CreateMap<HistoryTransactionDto, HistoryTransaction>()
                .ForMember(d =>
                    d.TransactionType, opt =>
                    opt.MapFrom(source => Enum.GetName(typeof(TransactionType),
                    source.TransactionType)))
                .ForMember(d =>
                    d.TransactionNetworkType, opt =>
                    opt.MapFrom(source => Enum.GetName(typeof(TransactionNetworkType),
                    source.TransactionNetworkType)));

var historyModel = mapper.Map<HistoryTransaction>(history);

var historyDto = mapper.Map<HistoryTransactionDto>(historyModel);

public enum TransactionType 
{
    Withdrawal, Buy, Send
}
You don't need the MapFrom, enum mapping is built in. A repro would help. Make a gist that we can execute and see fail. - Lucian Bargaoanu