0
automapper
をado.net
として使用する適切な方法は、一般的な方法でasp.net core
ですか? Sqlクエリの列名が<T>
の場合と同じです。指定された変数の例では、result
は常に空リストです。したがって、automapperはオブジェクトプロパティをDbDataReader列にマップできませんでした。ADO.NETとAutoMapperを使用したASP.NET CORE
public class CustomDbContext : BaseRepository
{
readonly DbConnection dbConn;
public CustomDbContext(RepoDbContext context) : base(context)
{
dbConn = context.Database.GetDbConnection();
}
public async Task<List<T>> Get<T>(string sql) where T : class
{
var config = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.CreateMap<DbDataReader, List<T>>();
});
var mapper = config.CreateMapper();
await dbConn.OpenAsync();
using (var command = dbConn.CreateCommand())
{
command.CommandText = sql;
var reader = await command.ExecuteReaderAsync();
var result = new List<T>();
if (reader.HasRows)
{
await reader.ReadAsync();
result = mapper.Map<DbDataReader, List<T>>(reader);
}
reader.Dispose();
return result;
}
}
}
さらに詳しいオートマッパ設定を指定する必要がありますか、このようにすることはできませんか?
この場合、mapper.Map>(読者) 'はヌルを返します。 –
Brivvirs