OData & [Queryable] Attribute

Topics: ASP.NET Web API
Sep 6, 2012 at 11:37 AM

I built an app using the MVC4 Beta and was happily using the available built in OData functionality. Knowing that it was removed in the final release, I've updated my solution to the release version of MVC4, installed the latest pre-release of Microsoft.AspNet.WebApi.OData and decorated my Get methods with the [Queryable] attribute.

I made the assumption that unless I wanted to do anything "fancy", I should be good to go...

However when making calls with an OData querystring (e.g. ?$top=100) i'm getting exceptions every time.

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "The given key was not present in the dictionary.",
    "ExceptionType": "System.Collections.Generic.KeyNotFoundException",
    "StackTrace": "   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)\r\n   at System.Web.Http.OData.Builder.EdmTypeBuilder.CreateStructuralTypeBody(EdmStructuredType type, IStructuralTypeConfiguration config)\r\n   at System.Web.Http.OData.Builder.EdmTypeBuilder.CreateEntityTypeBody(EdmEntityType type, IEntityTypeConfiguration config)\r\n   at System.Web.Http.OData.Builder.EdmTypeBuilder.CreateEdmTypeBody(IStructuralTypeConfiguration config)\r\n   at System.Web.Http.OData.Builder.EdmTypeBuilder.<GetEdmTypes>d__0.MoveNext()\r\n   at System.Linq.Enumerable.<OfTypeIterator>d__aa`1.MoveNext()\r\n   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)\r\n   at System.Linq.Enumerable.ToDictionary[TSource,TKey](IEnumerable`1 source, Func`2 keySelector)\r\n   at System.Web.Http.OData.Builder.EdmModelHelperMethods.BuildEdmModel(String containerNamespace, String containerName, IEnumerable`1 entityTypeConfigurations, IEnumerable`1 entitySetConfigurations)\r\n   at System.Web.Http.OData.Builder.ODataModelBuilder.GetEdmModel()\r\n   at System.Web.Http.OData.Builder.ODataConventionModelBuilder.GetEdmModel()\r\n   at System.Web.Http.HttpActionDescriptorExtensions.<>c__DisplayClass1.<GetEdmModel>b__0(Object _)\r\n   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)\r\n   at System.Web.Http.HttpActionDescriptorExtensions.GetEdmModel(HttpActionDescriptor actionDescriptor, Type entityClrType)\r\n   at System.Web.Http.QueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.CallOnActionExecuted(HttpActionContext actionContext, HttpResponseMessage response, Exception exception)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<>c__DisplayClass2.<System.Web.Http.Filters.IActionFilter.ExecuteActionFilterAsync>b__0(HttpResponseMessage response)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass41`2.<Then>b__40(Task`1 t)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.ThenImpl[TTask,TOuterResult](TTask task, Func`2 continuation, CancellationToken cancellationToken, Boolean runSynchronously)"
}

Knowing that the underlying codebase has changed, I guess my question is:

Should I expect that my CodeFirst POCO entities that worked previously to work using the WebApi.OData package (0.1.0-alpha-120815)?

Am I encountering bugs in the pre-release, or do I need to explicitly build an ODataModel?

Sep 6, 2012 at 6:13 PM

This is a known issue with the WebApi.OData package (0.1.0-alpha-120815) package that we have fixed. We are publishing nightly builds of the odata package on myget. You can pick up the latest nightly build. More about how to use the nightly builds here 

http://blogs.msdn.com/b/henrikn/archive/2012/06/01/using-nightly-asp-net-web-stack-nuget-packages-with-vs-2012-rc.aspx

 

Thanks.