1

将 JSONAPI.NET 升级到最新版本(0.3.0-pre-1)后,我无法查询单个资源(即 /{controller}/{id})。我可以查询所有多个资源端点(即/{controller})。

它可能与 相关,JSONAPI.EntityFramework.Http.ApiController因为我已经使用创建了一个控制器System.Web.Http.ApiController并且它工作正常。我使用了相同的 DbContext。

我已经关注/尝试了JSONAPI.TodoMVC.API中的所有代码。

这是我得到的错误:

{
"errors": [
    {
        "id": "6b49c8a9-d7b4-473e-80cd-c771fe1e7d13",
        "status": "500",
        "title": "System.AggregateException",
        "detail": "One or more errors occurred.",
        "inner": {
            "id": null,
            "status": "500",
            "title": "System.ArgumentException",
            "detail": "The Type AssetClass was not found in the DbContext with Type ApiContext",
            "inner": {
                "id": null,
                "status": "500",
                "title": "System.ArgumentException",
                "detail": "The member with identity 'ConeTec.DataServices.Web.Areas.WebApi.Models.AssetClass' does not exist in the metadata collection.\r\nParameter name: identity",
                "inner": null,
                "stackTrace": "   at System.Data.Entity.Core.Metadata.Edm.ItemCollection.GetItem[T](String identity, Boolean ignoreCase)\r\n   at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetKeyNames(Type type)"
            },
            "stackTrace": "   at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetKeyNames(Type type)\r\n   at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetKeyProperties(Type type)\r\n   at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetByIdAsync(Type type, Object[] idValues)\r\n   at JSONAPI.EntityFramework.EntityFrameworkMaterializer.<GetByIdAsync>d__0`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at JSONAPI.Http.ApiController`1.<Get>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()"
        }
   }
]

}

知道为什么我会收到此错误吗?

谢谢!

S'pht'Kr @S'pht'Kr

csantero @csantero

这是完整的错误跟踪:

{
"errors": [
    {
        "id": "57a5f40e-b170-407f-982a-c7cad64539e9",
        "status": "500",
        "title": "System.AggregateException",
        "detail": "One or more errors occurred.",
        "inner": {
            "id": null,
            "status": "500",
            "title": "System.ArgumentException",
            "detail": "The Type AssetClass was not found in the DbContext with Type ApiContext",
            "inner": {
                "id": null,
                "status": "500",
                "title": "System.ArgumentException",
                "detail": "The member with identity 'ConeTec.DataServices.Web.Areas.WebApi.Models.AssetClass' does not exist in the metadata collection.\r\nParameter name: identity",
                "inner": null,
                "stackTrace": "   at System.Data.Entity.Core.Metadata.Edm.ItemCollection.GetItem[T](String identity, Boolean ignoreCase)\r\n   at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetKeyNames(Type type)"
            },
            "stackTrace": "   at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetKeyNames(Type type)\r\n   at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetKeyProperties(Type type)\r\n   at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetByIdAsync(Type type, Object[] idValues)\r\n   at JSONAPI.EntityFramework.EntityFrameworkMaterializer.<GetByIdAsync>d__0`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at JSONAPI.Http.ApiController`1.<Get>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()"
        },
        "stackTrace": "   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)\r\n   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)\r\n   at System.Threading.Tasks.Task.Wait()\r\n   at ConeTec.DataServices.Web.Filters.ApiUserContextAttribute.ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func`1 continuation) in c:\\CDS\\ConeTecGeoDb\\ConeTec.DataServices.Web\\Filters\\UserContextAttribute.cs:line 160\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
    }
]

}

这是我的一个例子AssetClassesController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ConeTec.DataServices.Web.Areas.WebApi.Models;
using JSONAPI.EntityFramework.Http;

namespace ConeTec.DataServices.Web.Areas.WebApi.Controllers {
    public class AssetClassesController : ApiController<AssetClass, ApiContext> {

    }
}
4

3 回答 3

1

此错误应从https://github.com/SphtKr/JSONAPI.NET/pull/77开始修复。

于 2015-02-24T01:37:41.363 回答
0

确保在 ApiContext 类中将 AssetClass 定义为DBSet<AssetClass> AssetClass

于 2015-02-13T20:06:18.650 回答
0

好的,我正在空中编码……在我的 iPhone 上……但试一试。

创建您自己的子类EntityFrameworkMaterializer并覆盖该GetKeyNames方法,如下所示:

protected override IEnumerable<string> GetKeyNames(Type type)
{
    this.DbContext.MetadataWorkspace.LoadFromAssembly(this.DbContext.GetType().Assembly);
    return base.GetKeyNames(type);
}

然后在你的ApiController覆盖...

protected override JSONAPI.Core.IMaterializer MaterializerFactory()
{
    return new MyMaterializer(new ApiContext());
}

这样做有一些效率低下,但如果这解决了你的问题,那么我可以弄清楚在图书馆做什么。

作为备用计划,只需使用重写的 GetKeyNames 方法返回每个模型类型的正确键名列表。(提示:如果这在 0.2.0 中对您有用,那么您new List<string> { "Id" }每次都可以安全地简单地 return ......因为我最近发现这就是旧的 GetKeyNames 方法所能做的一切。);-)

于 2015-02-14T20:03:58.617 回答