2013-05-14 113 views
0

我得到錯誤,而與模型automapper顯示錯誤「指定的轉換無效」。錯誤

這裏映射數據表是我的代碼

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

堆棧跟蹤低於

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

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

[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 OPTS)353
AutoMapper.MappingEngine.Map(TSource源)564
AutoMapper.Mapper.Map(TSource源)461
SkillKindleAdmin.Controllers.FeedbackController.ExcelUpload(HttpPostedFileBase 文件)在d:\技能 在線\樹幹\ SkillKindleAdmin \控制器\ FeedbackController.cs:57
lambda_method(Closure,ControllerBase,Object []) 107
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,對象[]參數)286
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary的參數)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
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(IA syncResult asyncResult,對象標籤)492個
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult的 asyncResult)262
System.Web.Mvc.Async。 <> c
_DisplayClass2a.b_ 20() +203 System.Web.Mvc.Async。 <>Ç _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()333
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult的asyncResult, 對象標籤)397
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult的asyncResult, 對象標籤)266
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult的asyncResult) 253的System.Web .Mvc.Async。 <>Ç
_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, 對象標籤)+397
System.Web.Mvc.Async.AsyncResultWrapper。端(IAsyncResult的asyncResult, 對象標籤)266
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一步,布爾& completedSynchronously)+603

回答

0

你確定數據讀取器返回的列與對象ExcelFeedbackRecord,inc。中的屬性字段完全匹配魯丁套管? 如果不是,則創建地圖時必須添加配置。

+0

首先感謝您的幫助,是的列完全匹配屬性字段,但它不是字符串類型。現在它是固定的 – rajansoft1 2013-05-15 05:04:23

0

那麼問題是,當我們將DataSet與模型映射時,模型的所有屬性都必須是字符串類型,然後才能使用automapper的CustomTypeConversion方法。

相關問題