我正在使用 automapper v4.1.1 和 automapper.data1.0.0.beta 1。我有控制台应用程序,我的映射代码如下所示:
Mapper.Initialize(cfg => {
MapperRegistry.Mappers.Add(new DataReaderMapper {YieldReturnEnabled = true}
);
cfg.CreateMap<IDataRecord, AircraftDetails>();
});
我的数据库调用代码和映射看起来像
var aircraft=new AircraftDetails();
using (SqlConnection connection =
new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[_connectionString].ConnectionString))
{
SqlCommand command =
new SqlCommand(storedProcedureAsString, connection);
command.AddInputParameters(new {a=aircraftId});
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
var res = Mapper.Map<IDataReader, IEnumerable<AircraftDetails>>(reader);
aircraft = res.FirstOrDefault();
}
}
return aircraft;
当我运行此代码时,我收到错误:
未处理的异常:System.InvalidCastException:指定的转换无效。在 DynamicCreate(IDataRecord ) 在 AutoMapper.Data.DataReaderMapper.d__10.MoveNext() 在 System.Linq.Enumerable.d__94
1.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable
1 源)
任何想法我做错了什么?
干杯
伊斯梅尔