0

Here's my issue, I'm trying to map both this entities, and I aways get an exception:

From:

public int IdCorpoGestor { get; private set; }
    public string Nome { get; private set; }
    public string Email { get; private set; }
    public string Federacao { get; private set; }
    public DateTime DataIniMandato { get; private set; }
    public DateTime DataFimMandato { get; private set; }
    public string Telefone1 { get; private set; }
    public string Telefone2 { get; private set; }
    public int IdConselho { get; private set; }
    [ForeignKey("IdConselho")]
    public Conselho Conselho { get; private set; }
    public int IdTipo { get; private set; }
    [ForeignKey("IdTipo")]
    public Indicador Tipo { get; private set; }
    public bool Ativo { get; private set; }
}

To:

public class CorpoGestorDTO
{
    public int IdCorpoGestor { get; set; }
    public string Nome { get; set; }
    public string Email { get; set; }
    public string Federacao { get; set; }
    public DateTime DataIniMandato { get; set; }
    public DateTime DataFimMandato { get; set; }
    public string Telefone1 { get; set; }
    public string Telefone2 { get; set; }
    public int IdConselho { get; set; }
    public int IdTipo { get; set; }
    public bool Ativo { get; set; }
    public string Tipo { get; set; }
}

Mapping:

 Mapper.Initialize(cfg => cfg.CreateMap<CorpoGestor, CorpoGestorDTO>()
            .ForMember(x => x.Tipo, y => y.MapFrom(s => s.Tipo.Nome)));

Calling Mapper from DataBase result:

Mapper.Map<IEnumerable<CorpoGestor>, List<CorpoGestorDTO>>(result);

Exception:

Missing type map configuration or unsupported mapping

EDIT

Openned an issue at the GitHub for AutoMapper, you can have more information there: Automapper 5.1.1 Can't map Complex object, aways invalid #1783

Fals
  • 6,813
  • 4
  • 23
  • 43

1 Answers1

1

Try the following:

Mapper.Initialize(cfg => 
{
    cfg.CreateMap<CorpoGestor, CorpoGestorDTO>();
    cfg.CreateMap<Indicador, string>().ConvertUsing(x=> x.Nome);
}

You need to convert one data type to another. To do that, the second line is added to your mapping configuration.

Also, you should only call this once. Doing it multiple times will overwrite previous configurations.

  • @Fals is using .ForMember(x => x.Tipo, y => y.MapFrom(s => s.Tipo.Nome)), so problem not in this field. – Alexander S. Nov 10 '16 at 09:53
  • @Amy Does't Work! Same problem persisting, even with this modification! – Fals Nov 10 '16 at 11:57
  • Doesn't work doesn't tell us anything useful. We need more information than that. –  Nov 10 '16 at 13:44
  • @Amy see the Edit, I provided full information on GitHub, and a Gist – Fals Nov 10 '16 at 13:59
  • @Amy just edit the answer saying that You should only call Mapper.Initialize once, that was my problem, I was overwriting my previous config with an invalid one – Fals Nov 11 '16 at 11:34
  • Ahh, that would do it. –  Nov 11 '16 at 14:04