最初,我收到此错误:
发生System.InvalidOperationException
HResult = -2146233079
Message =源IQueryable没有实现IDbAsyncEnumerable。只有实现IDbAsyncEnumerable的源才能用于Entity Framework异步操作。有关更多详细信息,请参见http://go.microsoft.com/fwlink/?LinkId=287068。Source = EntityFramework
StackTrace: 在System.Data.Entity.QueryableExtensions.AsDbAsyncEnumerable [T](IQueryable
1 source)
1来源)
at System.Data.Entity.QueryableExtensions.ToListAsync[TSource](IQueryable
在Cpr.Apps.Tam.Model.Utilities.DataSnapshot`1.d__18.MoveNext()中的C:\ Projects \ MyApp \ MyAppModel \ Cpr.Apps.Tam.Model \ Utilities \ DataSnapshot.cs:line 160InnerException:
因此,我尝试使用此代码(请参阅他引用的页面@Tony O'Hagan's中的IDbAsyncEnumerable not implemented的https://msdn.microsoft.com/en-us/data/dn314429#async答案):
这是源代码:
public static class AsyncQueryableExtensions
{
public static IQueryable<TElement> AsAsyncQueryable<TElement>(this IEnumerable<TElement> source)
{
return new DbAsyncEnumerable<TElement>(source);
}
public static IDbAsyncEnumerable<TElement> AsDbAsyncEnumerable<TElement>(this IEnumerable<TElement> source)
{
return new DbAsyncEnumerable<TElement>(source);
}
public static EnumerableQuery<TElement> AsAsyncEnumerableQuery<TElement>(this IEnumerable<TElement> source)
{
return new DbAsyncEnumerable<TElement>(source);
}
public static IQueryable<TElement> AsAsyncQueryable<TElement>(this Expression expression)
{
return new DbAsyncEnumerable<TElement>(expression);
}
public static IDbAsyncEnumerable<TElement> AsDbAsyncEnumerable<TElement>(this Expression expression)
{
return new DbAsyncEnumerable<TElement>(expression);
}
public static EnumerableQuery<TElement> AsAsyncEnumerableQuery<TElement>(this Expression expression)
{
return new DbAsyncEnumerable<TElement>(expression);
}
}
internal class DbAsyncQueryProvider<TEntity> : IDbAsyncQueryProvider
{
private readonly IQueryProvider _inner;
internal DbAsyncQueryProvider(IQueryProvider inner)
{
_inner = inner;
}
public IQueryable CreateQuery(Expression expression)
{
return new DbAsyncEnumerable<TEntity>(expression);
}
public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
{
return new DbAsyncEnumerable<TElement>(expression);
}
public object Execute(Expression expression)
{
return _inner.Execute(expression);
}
public TResult Execute<TResult>(Expression expression)
{
return _inner.Execute<TResult>(expression);
}
public Task<object> ExecuteAsync(Expression expression, CancellationToken cancellationToken)
{
return Task.FromResult(Execute(expression));
}
public Task<TResult> ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken)
{
return Task.FromResult(Execute<TResult>(expression));
}
}
internal class DbAsyncEnumerable<T> : EnumerableQuery<T>, IDbAsyncEnumerable<T>, IQueryable<T>
{
public DbAsyncEnumerable(IEnumerable<T> enumerable)
: base(enumerable)
{ }
public DbAsyncEnumerable(Expression expression)
: base(expression)
{ }
public IDbAsyncEnumerator<T> GetAsyncEnumerator()
{
return new DbAsyncEnumerator<T>(this.AsEnumerable().GetEnumerator());
}
IDbAsyncEnumerator IDbAsyncEnumerable.GetAsyncEnumerator()
{
return GetAsyncEnumerator();
}
IQueryProvider IQueryable.Provider
{
get { return new DbAsyncQueryProvider<T>(this); }
}
}
internal class DbAsyncEnumerator<T> : IDbAsyncEnumerator<T>
{
private readonly IEnumerator<T> _inner;
public DbAsyncEnumerator(IEnumerator<T> inner)
{
_inner = inner;
}
public void Dispose()
{
_inner.Dispose();
}
public Task<bool> MoveNextAsync(CancellationToken cancellationToken)
{
return Task.FromResult(_inner.MoveNext());
}
public T Current
{
get { return _inner.Current; }
}
object IDbAsyncEnumerator.Current
{
get { return Current; }
}
}
请注意,我遇到以下错误:
System.NullReferenceException未通过用户代码HResult = -2147467261处理。Message =对象引用未设置为对象的实例。
Source =匿名托管的DynamicMethods程序集StackTrace:
at lambda_method(Closure,DefectSummary)
在System.Linq.Enumerable.WhereListIterator1.MoveNext()
1.MoveNext()
at System.Linq.Enumerable.<DistinctIterator>d__64
处 在System.Linq.Buffer1..ctor(IEnumerable
1处)
在System.Linq.OrderedEnumerable1.<GetEnumerator>d__1.MoveNext()
1.MoveNext()处
at System.Linq.Enumerable.<DistinctIterator>d__64
在System.Linq.Enumerable.d__251.MoveNext()
1.MoveNextAsync(CancellationToken cancelleToken)中的C:\ Projects \ MyApp \ Model \ Utilities \ AsyncQueryableExtensions.cs:125行
at MyApp.Model.Utilities.DbAsyncEnumerator
在System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.d__5`1.MoveNext()InnerException:
是否有人建议我如何找到导致异常的空值?发生在此功能中:
public Task<bool> MoveNextAsync(CancellationToken cancellationToken)
{
return Task.FromResult(_inner.MoveNext());
}
当我检查_inner时,我不清楚变量的哪一部分导致了空错误:
- _inner {System.Linq.Enumerable.<TakeIterator>d__25<MyApp.Model.Entities.DefectSummary>} System.Collections.Generic.IEnumerator<MyApp.Model.Entities.DefectSummary> {System.Linq.Enumerable.<TakeIterator>d__25<MyApp.Model.Entities.DefectSummary>}
+ Non-Public members
- Results View Expanding the Results View will enumerate the IEnumerable
- Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
Count 0 int
IsFixedSize false bool
IsReadOnly false bool
IsSynchronized false bool
- Keys {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} System.Collections.ICollection {System.Collections.ListDictionaryInternal.NodeKeyValueCollection}
+ Non-Public members
- Results View Expanding the Results View will enumerate the IEnumerable
Empty "Enumeration yielded no results" string
SyncRoot {object} object
+ Values {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} System.Collections.ICollection {System.Collections.ListDictionaryInternal.NodeKeyValueCollection}
+ Non-Public members
+ Results View Expanding the Results View will enumerate the IEnumerable
HResult -2147467261 int
HelpLink null string
+ InnerException null System.Exception
Message "Object reference not set to an instance of an object." string
Source "Anonymously Hosted DynamicMethods Assembly" string
StackTrace " at lambda_method(Closure , DefectSummary )\r\n at System.Linq.Enumerable.WhereListIterator`1.MoveNext()\r\n at System.Linq.Enumerable.<DistinctIterator>d__64`1.MoveNext()\r\n at System.Linq.Buffer`1..ctor(IEnumerable`1 source)\r\n at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()\r\n at System.Linq.Enumerable.<DistinctIterator>d__64`1.MoveNext()\r\n at System.Linq.Enumerable.<TakeIterator>d__25`1.MoveNext()\r\n at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()" string
+ TargetSite {Boolean lambda_method(System.Runtime.CompilerServices.Closure, MyApp.Model.Entities.DefectSummary)} System.Reflection.MethodBase {System.Reflection.Emit.DynamicMethod.RTDynamicMethod}
+ Static members
+ Non-Public members
更新:
仅供参考,这是设置查询的高级功能:
public IQueryable<Defect> GetLoadQuery()
{
IQueryable<Defect> query = null;
// Call to stored procedure GetDefects
var defectList = DbContext.GetDefects(null, null).ToList();
var defects = new List<Defect>();
using (IDefectDataService dataService = ServiceLocator.Current.GetInstance<IDefectDataService>())
{
foreach (var i in defectList)
{
dataService.DefectId = i.NotificationNo;
NotificationLinearAsset notificationLam = dataService.GetNotificationLinearAsset();
decimal? startPoint = notificationLam.StartPoint;
decimal? endPoint = notificationLam.EndPoint;
defects .Add(new Defect()
{
NotificationNo = i.NotificationNo,
Priority = i.Priority,
ReportedBy = i.ReportedBy,
MeasuringDocument = i.MeasuringDocument,
AssetTypeDescription = i.AssetTypeDescription,
StartPoint = startPoint,
EndPoint = endPoint,
Description = i.Description,
AssetId = i.AssetId,
});
}
query = defectSummaries.AsQueryable();
// Other code here for filtering results...
}
return query.AsAsyncQueryable();
}
更新2:
它变得更加奇怪,因为如果我注释掉这两行,查询将起作用并返回结果:
//StartPoint = startPoint,
//EndPoint = endPoint,
但是,当foreach
或startPoint
为空时,当我在endPoint
中放置条件断点以停止时,调试器将永远不会停止。
更新3:
感谢您的建议@Ivan Stoev。请注意,我今天进行了另一项测试,除了取消注释StartPoint/EndPoint
之外,没有更改任何代码,并且该测试没有错误,因此我不确定为什么会这样。也许某些东西被缓存在Visual Studio中了?
答案 0 :(得分:0)
通过代替使用存储过程结果来解决此问题,该方法创建了GetDefect_Result
类型,然后将其转换为{{1}中的MVVM应用要使用的Defect
类型}}循环中,我创建了一个视图并使用该视图的结果,这些结果直接直接映射到foreach
类型的结果。此解决方案还涉及删除对Defect
的所有引用。
IDbAsyncEnumerable
仅供参考,我想使用存储过程,因为我正在使用的数据库通常无法优化视图(如果它具有子视图),但是最终我能够通过向关联表中添加更多索引来使用视图提高数据库性能。