简体   繁体   English

自动映射器显示错误“指定的转换无效。”

[英]automapper showing error “Specified cast is not valid.”

i am getting error while mapping DataTable with the model 在将DataTable与模型映射时出现错误

here is my code 这是我的代码

       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();
        }

stack trace for error is below 堆栈跟踪错误如下

  [InvalidCastException: Specified cast is not valid.] DynamicCreate(IDataRecord ) +1673 

AutoMapper.Mappers.DataReaderMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) +433 AutoMapper.Mappers.DataReaderMapper.Map(ResolutionContext上下文,IMappingEngineRunner映射器)+433
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +347 AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext上下文)+347

[AutoMapperMappingException: Trying to map System.Data.IDataReader to System.Collections.Generic.IList 1[[SkillKindle.BLL.Feedbacks.ExcelFeedbackRecord, SkillKindle.BLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +433 AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType, Action
[AutoMapperMappingException:尝试将System.Data.IDataReader映射到System.Collections.Generic.IList 1[[SkillKindle.BLL.Feedbacks.ExcelFeedbackRecord, SkillKindle.BLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +433 AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType, Action
1[[SkillKindle.BLL.Feedbacks.ExcelFeedbackRecord, SkillKindle.BLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +433 AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType, Action
1[[SkillKindle.BLL.Feedbacks.ExcelFeedbackRecord, SkillKindle.BLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +433 AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType, Action
1 opts) +353
1[[SkillKindle.BLL.Feedbacks.ExcelFeedbackRecord, SkillKindle.BLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +433 AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType, Action
1选择)+353

AutoMapper.MappingEngine.Map(TSource source) +564 AutoMapper.MappingEngine.Map(TSource源代码)+564
AutoMapper.Mapper.Map(TSource source) +461 AutoMapper.Mapper.Map(TSource源代码)+461
SkillKindleAdmin.Controllers.FeedbackController.ExcelUpload(HttpPostedFileBase file) in d:\\Skill Online\\trunk\\SkillKindleAdmin\\Controllers\\FeedbackController.cs:57 d:\\ Skill Online \\ trunk \\ SkillKindleAdmin \\ Controllers \\ FeedbackController.cs:57中的SkillKindleAdmin.Controllers.FeedbackController.ExcelUpload(HttpPostedFileBase文件)
lambda_method(Closure , ControllerBase , Object[] ) +107 lambda_method(Closure,ControllerBase,Object [])+107
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +286 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,Object []参数)+286
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2 parameters) +655
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary 2 parameters) +655
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) +655
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) +655
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) +326
2 parameters) +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 parameters) +317
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +219 System.Web.Mvc.Async.<>c__DisplayClass8
System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary 2 parameters) +317
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +219 System.Web.Mvc.Async.<>c__DisplayClass8
2 parameters) +317
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +219 System.Web.Mvc.Async.<>c__DisplayClass8
2 parameters) +317
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +219 System.Web.Mvc.Async.<>c__DisplayClass8
1.b__7(IAsyncResult ) +275 System.Web.Mvc.Async.WrappedAsyncResult 1.End() +328 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +492
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +261
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +268 System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +461 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +235 System.Web.Mvc.Async.WrappedAsyncResult
2 parameters) +317
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +219 System.Web.Mvc.Async.<>c__DisplayClass8
1.b__7(IAsyncResult )+275 System.Web.Mvc.Async.WrappedAsyncResult 1.End() +328 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +492
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +261
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +268 System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +461 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +235 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +328 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +492
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +261
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +268 System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +461 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +235 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +328 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +492
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +261
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +268 System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +461 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +235 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +328 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +492
1.End() +328 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +492
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +261
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +268 System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +461 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +235 System.Web.Mvc.Async.WrappedAsyncResult
1.End()+328 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,Object tag)+492

System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +262 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)+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.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +387
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +285
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +234
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +275 System.Web.Mvc.Async.WrappedAsyncResult
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.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +387
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +285
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +234
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +275 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +316 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +387
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +285
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +234
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +275 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +316 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +387
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +285
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +234
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +275 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +333
1.End() +316 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +387
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +285
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +234
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +275 System.Web.Mvc.Async.WrappedAsyncResult
1.End() 1.End() +316 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +387
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +285
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +234
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +275 System.Web.Mvc.Async.WrappedAsyncResult
333

System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +397 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,对象标记)+397
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +266 System.Web.Mvc.Async.AsyncResultWrapper.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.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +397
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +266 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +254
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +226
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +230
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +275 System.Web.Mvc.Async.WrappedAsyncResult
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.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +397
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +266 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +254
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +226
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +230
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +275 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +333
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +397
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +266 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +254
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +226
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +230
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +275 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +333
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +397
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +266 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +254
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +226
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +230
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +275 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +333
1.End() +333
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +397
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +266 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +254
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +226
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +230
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +275 System.Web.Mvc.Async.WrappedAsyncResult
1.End() 1.End() +333
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +397
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +266 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +254
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +226
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +230
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +275 System.Web.Mvc.Async.WrappedAsyncResult
333

System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +397 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,对象标记)+397
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +266 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,对象标记)+266
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +255 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +225 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+255 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult结果)+225
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1086 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +603 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+1086 System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值且已完成)+603

Are you sure the columns returned by the datareader exactly match the property fields in the object ExcelFeedbackRecord, including casing? 您是否确定datareader返回的列与对象ExcelFeedbackRecord中的属性字段(包括大小写)完全匹配? If they are not, you'll have to add a configuration when creating the map. 如果不是,则在创建地图时必须添加配置。

很好的问题是,当我们将DataSet与模型映射时,模型的所有属性都必须仅是字符串类型,然后可以使用automapper的CustomTypeConversion方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM