0

Estou com esse erro de tipagem ao tentar mapear um SqlDataReader vindo de uma procedure, mapeando com o AutoMapper

Erro

An unhandled exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll

Additional information: Error mapping types

Segue meu DataReader

public List<T> DataReader<T>(string Procedure, List<SqlParameter> parameters)
{
    var rows = ExecuteProcedureReader(Procedure, parameters);

    if (rows.HasRows)
    {
        var config = new MapperConfiguration(cfg => { });
        var mapper = config.CreateMapper();

        return mapper.Map<IDataReader, List<T>>(rows);
    }

    return null;
}

Minha Classe

public class Usuario
{
    public int Codigo { get; set; }

    public string Nome { get; set; }

    public string Emdereco { get; set; }

    public string Cpf { get; set; }
}

Chamada do meu método

var lista = conn.DataReader<Usuario>("PR_USUARIO_CONSULTAR", param);

foreach (var item in lista)
{
    Console.WriteLine(item.Nome);
}
1
  • Onde está e como está sendo feita a inicialização do AutoMapper? 28/06/2019 às 16:30

1 Resposta 1

0

O problema foi resolvido configurando o AutoMapper para que ele mapeie dinamicamente o objeto, através do CreateMissingTypeMaps.

Segue o Código:

public List<T> DataReader<T>(string Procedure, List<SqlParameter> parameters)
{
    var rows = ExecuteProcedureReader(Procedure, parameters);

    if (rows.HasRows)
    {
        var config = new MapperConfiguration(cfg => { cfg.CreateMissingTypeMaps = true; });
        var mapper = config.CreateMapper();

        return mapper.Map<IDataReader, List<T>>(rows);
    }

    return null;
}
1
  • Por que você não cria o mapeamento em vez de fazer isso aí? Mapper.CreateMap<IDataReader, List<UmTipoQueVocêUsa>>();? 28/06/2019 às 16:28

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .