C# 自动映射显示错误“;指定的强制转换无效;

C# 自动映射显示错误“;指定的强制转换无效;,c#,asp.net-mvc,automapper,fluentvalidation,C#,Asp.net Mvc,Automapper,Fluentvalidation,使用模型映射DataTable时出错 这是我的密码 if (file.ContentLength > 0) { var extension = Path.GetExtension(file.FileName); if (extension != null && extension.ToLower() != ".xlsx") { return

使用模型映射DataTable时出错

这是我的密码

       if (file.ContentLength > 0)
        {
            var extension = Path.GetExtension(file.FileName);
            if (extension != null && extension.ToLower() != ".xlsx")
            {
                return "please upload file with extension .xlsx";
            }

            Stream stream = file.InputStream;
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            excelReader.IsFirstRowAsColumnNames = true;
            DataSet result = excelReader.AsDataSet();
            if (result.Tables.Count > 0)
            {
                Mapper.Reset();
                Mapper.CreateMap<IDataReader, ExcelFeedbackRecord>();

                var results = Mapper.Map<IDataReader, IList<ExcelFeedbackRecord>>(result.Tables[0].CreateDataReader());

            }
            return result.Tables[0].Rows.Count.ToString();
        }
AutoMapper.Mappers.DataReaderMapper.Map(ResolutionContext上下文, IMappingEngineRunner映射器)+433
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext 上下文)+347

[AutoMapperMappingException:尝试将System.Data.IDataReader映射到 System.Collections.Generic.IList
1[[SkillKindle.BLL.Feedbacks.ExcelFeedbackRecord,
SkillKindle.BLL,版本=1.0.0.0,区域性=中性,
PublicKeyToken=null]]。类型为的异常
“AutoMapper.AutoMappingException”被抛出。]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext 上下文)+433 AutoMapper.MappingEngine.Map(对象源,类型 sourceType,Type destinationType,Action
1选项)+353
AutoMapper.MappingEngine.Map(TSource-source)+564
AutoMapper.Mapper.Map(TSource源)+461
SkillKindleAdmin.Controllers.FeedbackController.ExcelUpload(HttpPostedFileBase 文件)在d:\Skill中 在线\trunk\SkillKindleAdmin\Controllers\FeedbackController.cs:57
lambda_方法(闭包、控制器库、对象[])+107
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,对象[]参数)+286
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary
2参数)+655
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor ActionDescriptor,IDictionary
2 参数)+326
System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext,ActionDescriptor ActionDescriptor,IDictionary
2
参数)+317
System.Web.Mvc.Async.c__DisplayClass42.b__41() +219 System.Web.Mvc.Async.c__显示类8
1.b__7(IAsyncResult )+275 System.Web.Mvc.Async.WrappedAsyncResult
1.End()+328 System.Web.Mvc.Async.AsyncResultRapper.End(IAsyncResult asyncResult,
对象标记)+492
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult 异步结果)+261
System.Web.Mvc.Async.c__显示类39.b__33() +268 System.Web.Mvc.Async.c__显示类4f.b__49() +461 System.Web.Mvc.Async.c_uudisplayClass37.b_uu36(IAsyncResult asyncResult)+235 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +328 System.Web.Mvc.Async.AsyncResultRapper.End(IAsyncResult asyncResult,对象标记)+492
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult 异步结果)+262
System.Web.Mvc.Async.c_DisplayClass2a.b_20() +203 System.Web.Mvc.Async.c_DisplayClass25.b_22(IAsyncResult asyncResult)+392 System.Web.Mvc.Async.WrappedAsyncResult
1.End()
+316 System.Web.Mvc.Async.AsyncResultRapper.End(IAsyncResult asyncResult,对象标记)+387
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult 异步结果)+285
System.Web.Mvc.c__;u displayClassId.b_;u 18(IAsyncResult 异步结果)+234
System.Web.Mvc.Async.c\uuu DisplayClass4.b\uu 3(IAsyncResult ar)+275 System.Web.Mvc.Async.WrappedAsyncResult
1.End()+333
System.Web.Mvc.Async.AsyncResultRapper.End(IAsyncResult asyncResult, 对象标记)+397
System.Web.Mvc.Async.AsyncResultRapper.End(IAsyncResult asyncResult, 对象标记)+266
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +253 System.Web.Mvc.Async.c_DisplayClass4.b__3(IAsyncResult ar)+275 System.Web.Mvc.Async.WrappedAsyncResult
1.End()+333
System.Web.Mvc.Async.AsyncResultRapper.End(IAsyncResult asyncResult, 对象标记)+397
System.Web.Mvc.Async.AsyncResultRapper.End(IAsyncResult asyncResult, 对象标记)+266 System.Web.Mvc.Controller.EndExecute(IAsyncResult 异步结果)+254
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult 异步结果)+226
System.Web.Mvc.c_uuudisplayClass8.b_uuu3(IAsyncResult 异步结果)+230
System.Web.Mvc.Async.c\uuu DisplayClass4.b\uu 3(IAsyncResult ar)+275 System.Web.Mvc.Async.WrappedAsyncResult
1.End()+333
System.Web.Mvc.Async.AsyncResultRapper.End(IAsyncResult asyncResult, 对象标记)+397
System.Web.Mvc.Async.AsyncResultRapper.End(IAsyncResult asyncResult, 对象标记)+266
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +255 System.Web.Mvc.MvcHandler.System.Web.IHTTPassynchandler.EndProcessRequest(IAsyncResult 结果)+225
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1086 System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+603


您确定datareader返回的列与对象ExcelFeedbackRecord中的属性字段(包括大小写)完全匹配吗?
如果没有,则在创建映射时必须添加配置。

问题是,当我们使用模型映射数据集时,模型的所有属性必须仅为字符串类型,然后您可以使用automapper的CustomTypeConversion方法。

首先感谢您的帮助,是的,列与属性字段完全匹配,但不是字符串类型。现在是固定的
       [InvalidCastException: Specified cast is not valid.]    DynamicCreate(IDataRecord ) +1673