1

我正在使用最新版本的 web api。我正在返回一个IQuerable值,并且收到此错误:

iisexpress.exe Error: 0 : Operation=QueryableAttribute.ActionExecuted, Status=200 (OK), Exception=System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at System.Web.Http.OData.Builder.EdmTypeBuilder.CreateStructuralTypeBody(EdmStructuredType type, IStructuralTypeConfiguration config)
   at System.Web.Http.OData.Builder.EdmTypeBuilder.CreateEntityTypeBody(EdmEntityType type, IEntityTypeConfiguration config)
   at System.Web.Http.OData.Builder.EdmTypeBuilder.CreateEdmTypeBody(IStructuralTypeConfiguration config)
   at System.Web.Http.OData.Builder.EdmTypeBuilder.<GetEdmTypes>d__0.MoveNext()
   at System.Linq.Enumerable.<OfTypeIterator>d__aa`1.MoveNext()
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey](IEnumerable`1 source, Func`2 keySelector)
   at System.Web.Http.OData.Builder.EdmModelHelperMethods.BuildEdmModel(String containerNamespace, String containerName, IEnumerable`1 entityTypeConfigurations, IEnumerable`1 entitySetConfigurations)
   at System.Web.Http.OData.Builder.ODataModelBuilder.GetEdmModel()
   at System.Web.Http.OData.Builder.ODataConventionModelBuilder.GetEdmModel()
   at System.Web.Http.HttpActionDescriptorExtensions.<>c__DisplayClass1.<GetEdmModel>b__0(Object _)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at System.Web.Http.HttpActionDescriptorExtensions.GetEdmModel(HttpActionDescriptor actionDescriptor, Type entityClrType)
   at System.Web.Http.QueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
   at System.Web.Http.Tracing.Tracers.ActionFilterAttributeTracer.<>c__DisplayClass4.<OnActionExecuted>b__1()
   at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Action execute, Action`1 endTrace, Action`1 errorTrace)
iisexpress.exe Error: 0 : Operation=InteractionDataController.ExecuteAsync, Exception=System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at System.Web.Http.OData.Builder.EdmTypeBuilder.CreateStructuralTypeBody(EdmStructuredType type, IStructuralTypeConfiguration config)
   at System.Web.Http.OData.Builder.EdmTypeBuilder.CreateEntityTypeBody(EdmEntityType type, IEntityTypeConfiguration config)
   at System.Web.Http.OData.Builder.EdmTypeBuilder.CreateEdmTypeBody(IStructuralTypeConfiguration config)
   at System.Web.Http.OData.Builder.EdmTypeBuilder.<GetEdmTypes>d__0.MoveNext()
   at System.Linq.Enumerable.<OfTypeIterator>d__aa`1.MoveNext()
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey](IEnumerable`1 source, Func`2 keySelector)
   at System.Web.Http.OData.Builder.EdmModelHelperMethods.BuildEdmModel(String containerNamespace, String containerName, IEnumerable`1 entityTypeConfigurations, IEnumerable`1 entitySetConfigurations)
   at System.Web.Http.OData.Builder.ODataModelBuilder.GetEdmModel()
   at System.Web.Http.OData.Builder.ODataConventionModelBuilder.GetEdmModel()
   at System.Web.Http.HttpActionDescriptorExtensions.<>c__DisplayClass1.<GetEdmModel>b__0(Object _)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at System.Web.Http.HttpActionDescriptorExtensions.GetEdmModel(HttpActionDescriptor actionDescriptor, Type entityClrType)
   at System.Web.Http.QueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
   at System.Web.Http.Tracing.Tracers.ActionFilterAttributeTracer.<>c__DisplayClass4.<OnActionExecuted>b__1()
   at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Action execute, Action`1 endTrace, Action`1 errorTrace)
   at System.Web.Http.Tracing.Tracers.ActionFilterAttributeTracer.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
   at System.Web.Http.Filters.ActionFilterAttribute.CallOnActionExecuted(HttpActionContext actionContext, HttpResponseMessage response, Exception exception)
   at System.Web.Http.Filters.ActionFilterAttribute.<>c__DisplayClass2.<System.Web.Http.Filters.IActionFilter.ExecuteActionFilterAsync>b__0(HttpResponseMessage response)
   at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass41`2.<Then>b__40(Task`1 t)
   at System.Threading.Tasks.TaskHelpersExtensions.ThenImpl[TTask,TOuterResult](TTask task, Func`2 continuation, CancellationToken cancellationToken, Boolean runSynchronously)

有人可以解释一下问题是什么吗?

4

2 回答 2

0

我遇到了同样的错误。我将其追溯到 ODataModelBuilder 调用 GetEdmModel() 时。我正在使用 ODataConventionModelBuilder 并声明 EntitySets 来创建我的模型。就我而言,我有一个带有子集合的父类。我尝试了不同类型的集合,IList、List、ICollection、IEnumerable,但我继续收到此错误。我知道这不是最好的解决方案,但我将子集合声明为对象。我使用 Linq 将集合分配为 IEnumerable。我会继续研究这个,但至少我可以继续我的项目。我将不得不继续研究 ODataConventionModelBuilder。我希望这至少可以为您指明正确的方向。

-杰森

于 2012-09-06T17:14:38.050 回答
0

这似乎是 WebApi.OData 包 (0.1.0-alpha-120815) 中的一个已知问题。请参阅http://aspnetwebstack.codeplex.com/discussions/394626

于 2012-09-27T11:06:36.817 回答