Automappermappingexception: 'Error Mapping types. '

Asked

Viewed 1,981 times

1

Could you help me with this mistake described in the title, please. I am using the Automapper in the same way in other classes and only in those classes is it making this mistake. I couldn’t understand why of the error, since the Helpmodel and Helpviewmodel classes have equal properties. Automapper version is 7.0.1.

Helpmodel:

public class HelpModel
{
    public int Id { get; private set; }

    public string Area { get; private set; }

    public string Controller { get; private set; }

    public string Action { get; private set; }

    public string Template { get; private set; }




    public HelpModel()
    {
    }
}

Helpviewmodel:

public class HelpViewModel : BaseViewModel
{


    public int Id { get; set; }


    [Required(ErrorMessage = "Informe a {0} referente a Help")]
    [Display(Name = "Área")]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public string Area { get; set; }

    [Required(ErrorMessage = "Informe o {0} referente a Help")]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public string Controller { get; set; }

    [Required(ErrorMessage = "Informe a {0} referente a Help")]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public string Action { get; set; }

    [Required(ErrorMessage = "Preencha o {0} referente a Help")]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public string Template { get; set; }


    public HelpViewModel()
    {

    }
}

Where I use the Automapper:

 public List<HelpViewModel> Listar()
    {
        List<HelpModel> lstHelpModel = _helpRepository.List();

        List<HelpViewModel> lstHelpViewModel = Mapper.Map<List<HelpViewModel>>(lstHelpModel);

        return lstHelpViewModel;
    }

There must be something wrong I couldn’t figure out. Thanks.

  • Did you profile them? take a look at that answer: https://answall.com/questions/230671/asp-net-c-ddd-problema-ao-data-entidados-para-viewmodel/230679#230679, something may change because of the version

  • Thanks @Barbetta. This answer helped me a lot. With it I understood the Automapper statements better. In my case, we have a Mapconfig class where we initialize the map creating the necessary profiles.

1 answer

2

This statement was missing in the Map boot class:

config.CreateMap<HelpModel, HelpViewModel>();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.