自动映射器显示错误“指定的演员表无效。”

Posted

技术标签:

【中文标题】自动映射器显示错误“指定的演员表无效。”【英文标题】:automapper showing error "Specified cast is not valid." 【发布时间】:2013-05-08 11:34:04 【问题描述】:

在将 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();
        

错误的堆栈跟踪如下

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

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

[AutoMapperMappingException: 试图将 System.Data.IDataReader 映射到 System.Collections.Generic.IList1[[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, Action1 选择)+353 AutoMapper.MappingEngine.Map(TSource 源) +564 AutoMapper.Mapper.Map(TSource 源) +461 SkillKindleAdmin.Controllers.FeedbackController.ExcelUpload(HttpPostedFileBase 文件)在 d:\Skill Online\trunk\SkillKindleAdmin\Controllers\FeedbackController.cs:57 lambda_method(闭包, ControllerBase, Object[]) +107 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,Object[] 参数)+286 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext 控制器上下文,IDictionary2 parameters) +655 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 参数)+326 System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +317 System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +219 System.Web.Mvc.Async.<>c__DisplayClass81.b__7(IAsyncResult ) +275 System.Web.Mvc.Async.WrappedAsyncResult1.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.WrappedAsyncResult1.End() +328 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, 对象标签) +492 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.WrappedAsyncResult1.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.WrappedAsyncResult1.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.c_DisplayClass4.b__3(IAsyncResult ar) +275 System.Web.Mvc.Async.WrappedAsyncResult1.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.WrappedAsyncResult1.End() +333 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, 对象标签)+397 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 结果)+225 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1086 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +603

【问题讨论】:

【参考方案1】:

您确定数据读取器返回的列与对象 ExcelFeedbackRecord 中的属性字段完全匹配,包括大小写? 如果不是,则必须在创建地图时添加配置。

【讨论】:

首先感谢您的帮助,是的,列与属性字段完全匹配,但它不是字符串类型。现在它固定了【参考方案2】:

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

【讨论】:

以上是关于自动映射器显示错误“指定的演员表无效。”的主要内容,如果未能解决你的问题,请参考以下文章

对象引用未设置为对象实例的自动映射器问题

使用自动映射器映射对象列表

简单的自动映射器示例

无法使用自动映射器映射内部导航属性。 EF 核心

keycloak 从自定义协议映射器抛出身份验证错误

防止 Mapstruct 在自动映射器检测中使用方法