如何解决EF6 抛出指定的强制转换对异步方法无效
当使用 await
和 FirstOrDefaultAsync
时,我得到指定的强制转换无效异常。
var a = Db.Table1.sqlQuery("select * from Table1 where rownum =1 ").FirstOrDefault();
var b = await Db.Table1.sqlQuery("select * from Table1 where rownum =1 ").FirstOrDefaultAsync();
所以 a 很好 b 抛出错误。我错过了什么?
at System.Data.Common.DbDataReader.GetFieldValue[T](Int32 ordinal)
at System.Data.Common.DbDataReader.GetFieldValueAsync[T](Int32 ordinal,CancellationToken cancellationToken)
at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.<ReadIntAsync>d__25.MoveNext()
at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter.GetResult()
at System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.<InitializeAsync>d__4.MoveNext()
at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Core.Objects.Internal.BufferedDataReader.<InitializeAsync>d__0.MoveNext()
at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Core.Objects.ObjectContext.<ExecuteStoreQueryInternalAsync>d__73`1.MoveNext()
at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
at System.Data.Entity.Core.Objects.ObjectContext.<ExecuteInTransactionAsync>d__3d`1.MoveNext()
at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
at System.Data.Entity.Infrastructure.DbExecutionStrategy.<ProtectedExecuteAsync>d__9`1.MoveNext()
解决方法
如果有人遇到这个问题,我会发布一个答案。 Table1 EF 模型中缺少某些属性,或者换句话说,表 Table1 有额外的字段。为什么这不会导致 FirstOrDefault 并且只导致 FirstOrDefaultAsync 错误我不知道,可能是不同的实现。所以解决办法是同步Table1 DB表和Table1 EF模型
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。