entity-framework - 将 JSONAPI.NET 升级到最新版本( 0.3.0前 1 ) 后,无法查询单个资源

  显示原文与译文双语对照的内容
0 0

无法查询单个资源( 例如 。 /{controller}/{id}) 升级到最新版本( 0.3.0 -pre-1 ) 后的JSONAPI.NET 。 我可以查询所有的多个资源端点( 例如 。 /{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.rnParameter name: identity",
"inner": null,
"stackTrace":" at System.Data.Entity.Core.Metadata.Edm.ItemCollection.GetItem[T](String identity, Boolean ignoreCase)rn at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetKeyNames(Type type)"
 },
"stackTrace":" at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetKeyNames(Type type)rn at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetKeyProperties(Type type)rn at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetByIdAsync(Type type, Object[] idValues)rn at JSONAPI.EntityFramework.EntityFrameworkMaterializer.<GetByIdAsync>d__0`1.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at JSONAPI.Http.ApiController`1.<Get>d__0.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()"
 }
 }
]

}

为什么我得到这个错误?

谢谢!

'pht' @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.rnParameter name: identity",
"inner": null,
"stackTrace":" at System.Data.Entity.Core.Metadata.Edm.ItemCollection.GetItem[T](String identity, Boolean ignoreCase)rn at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetKeyNames(Type type)"
 },
"stackTrace":" at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetKeyNames(Type type)rn at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetKeyProperties(Type type)rn at JSONAPI.EntityFramework.EntityFrameworkMaterializer.GetByIdAsync(Type type, Object[] idValues)rn at JSONAPI.EntityFramework.EntityFrameworkMaterializer.<GetByIdAsync>d__0`1.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at JSONAPI.Http.ApiController`1.<Get>d__0.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()"
 },
"stackTrace":" at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)rn at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)rn at System.Threading.Tasks.Task.Wait()rn at ConeTec.DataServices.Web.Filters.ApiUserContextAttribute.ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func`1 continuation) in c:CDSConeTecGeoDbConeTec.DataServices.WebFiltersUserContextAttribute.cs:line 160rn at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn 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> {

 }
}

时间: 原作者:

0 0
0 0

确保你的ApiContext类中定义了AssetClass作为 DBSet<AssetClass> AssetClass

原作者:
0 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" } 每次。"。"。因为我最近发现了旧的GetKeyNames方法。

...