Error When Mapping Idatareader with Automapper

Asked

Viewed 167 times

0

I have this typing error when trying to map a Sqldatareader coming from a previous, mapping with Automapper

Error

An unhandled Exception of type 'Automapper.Automappermappingexception' occurred in Automapper.dll

Additional information: Error Mapping types

Follow my 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;
}

My Class

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

    public string Nome { get; set; }

    public string Emdereco { get; set; }

    public string Cpf { get; set; }
}

Call of my method

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

foreach (var item in lista)
{
    Console.WriteLine(item.Nome);
}

1 answer

0


The problem was solved by configuring the Automapper so that it dynamically changes the object, through the CreateMissingTypeMaps.

Follows the Code:

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;
}
  • Why don’t you create the mapping instead of doing it there? Mapper.CreateMap<IDataReader, List<UmTipoQueVocêUsa>>();?

Browser other questions tagged

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