我在URI中使用的任何OData命令,都会显示以下错误!
当我使用没有任何配置/参数OData的api时,效果很好!
但是,任何$filter
,$top
或$skip
都会产生以下错误:
ApiController
[Queryable]
public IQueryable<Processo> get()
{
return _repositorio.Query<Processo>();
}
Api路线
public static void Register(HttpConfiguration config)
{
// Controller Only -> To handle routes like `/api/pessoas`
config.Routes.MapHttpRoute(
name: "ControllerOnly",
routeTemplate: "api/{controller}"
);
// Controller with ID -> To handle routes like `/api/pessoas/1`
config.Routes.MapHttpRoute(
name: "ControllerAndId",
routeTemplate: "api/{controller}/{id}",
defaults: null,
constraints: new { id = @"^\d+$" } // Only integers
);
// Controllers with Actions -> To handle routes like `/api/pessoas/getProfissoes`
config.Routes.MapHttpRoute(
name: "ControllerAndAction",
routeTemplate: "api/{controller}/{action}"
);
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
IRepository查询工具
public IQueryable<T> Query<T>(params Expression<Func<T, object>>[] includeProperties)
where T : class, IEntity
{
return includeProperties.Aggregate<Expression<Func<T, object>>, IQueryable<T>>(Set<T>(), (current, includeProperty) => current.Include(includeProperty));
}
[已添加] Processo课程
public class Processo : IEntity, IAuditable
{
public DateTime CreateAt { get; set; }
public string CreateBy { get; set; }
public DateTime? UpdateAt { get; set; }
public string UpdateBy { get; set; }
public int Id { get; set; }
public DateTime DataEntrada { get; set; }
public virtual Modalidade Modalidade { get; set; }
public virtual PassoExecucao PassoAtual { get; set; }
public string Imovel { get; set; }
public double ValorFinanciado { get; set; }
public double ValorVenda { get; set; }
public double? FGTS { get; set; }
public virtual Pessoa Comprador { get; set; }
public virtual Pessoa Proprietario { get; set; }
public virtual Agencia Agencia { get; set; }
public virtual Empresa Despachante { get; set; }
public virtual Empresa Originador { get; set; }
public virtual ICollection<File> Arquivos { get; set; }
public virtual ICollection<Historico> Historicos { get; set; }
}
/api/processos?$filter=Id eq 1
错误相同 /api/processos?$skip=1
错误相同 /api/processos
没有错误!! [已添加]进行一些测试。发现只发生在引用Person类的地方。
public class Pessoa : IEntity, IAuditable
{
public int Id { get; set; }
public DateTime CreateAt { get; set; }
public string CreateBy { get; set; }
public DateTime? UpdateAt { get; set; }
public string UpdateBy { get; set; }
public string Nome { get; set; }
public DateTime? DataNascimento { get; set; }
public GeneroEnum Genero { get; set; }
public virtual TipoPessoa Tipo { get; set; }
public virtual Pessoa Conjuge { get; set; }
public virtual Nacionalidade Nacionalidade { get; set; }
public string Naturalidade { get; set; }
public virtual Profissao Profissao { get; set; }
public string Empresa { get; set; }
public double? RendaBrutaMensal { get; set; }
public DateTime? DataInicioEmpresa { get; set; }
public DateTime? DataReferenciaEmpresa { get; set; }
public EscolaridadeEnum Escolaridade { get; set; }
public string CPF { get; set; }
public string PIS { get; set; }
public string RG { get; set; }
public string OrgaoExpedidor { get; set; }
public string TipoDocumento { get; set; }
public DateTime? DataEmissao { get; set; }
public EstadoCivilEnum? EstadoCivil { get; set; }
public RegimeCasamentoEnum? RegimeCasamento { get; set; }
public bool? ComposicaoRenda { get; set; }
public bool? UniaoEstavel { get; set; }
public virtual ICollection<Telefone> Telefones { get; set; }
public virtual ICollection<Endereco> Enderecos { get; set; }
public virtual ICollection<Email> Emails { get; set; }
}
使用Processo类
[Queryable]
public IQueryable<Processo> get()
{
return _repositorio.Query<Processo>();
}
{
"$id": "1",
"Message": "An error has occurred.",
"ExceptionMessage": "The property does not belong to the specified type.\r\nParameter name: navigationProperty",
"ExceptionType": "System.ArgumentException",
"StackTrace": " at System.Web.Http.OData.Builder.EntityTypeConfiguration.AddNavigationProperty(PropertyInfo navigationProperty, EdmMultiplicity multiplicity)\r\n at System.Web.Http.OData.Builder.ODataConventionModelBuilder.MapEntityType(IEntityTypeConfiguration entity)\r\n at System.Web.Http.OData.Builder.ODataConventionModelBuilder.AddEntity(Type type)\r\n at System.Web.Http.OData.Builder.EntityTypeConfiguration.AddNavigationProperty(PropertyInfo navigationProperty, EdmMultiplicity multiplicity)\r\n at System.Web.Http.OData.Builder.ODataConventionModelBuilder.MapEntityType(IEntityTypeConfiguration entity)\r\n at System.Web.Http.OData.Builder.ODataConventionModelBuilder.MapTypes()\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)"
}
使用Pessoa课程
[Queryable]
public IQueryable<Pessoa> get()
{
return _repositorio.Query<Pessoa>();
}
{
"$id": "1",
"Message": "An error has occurred.",
"ExceptionMessage": "An item with the same key has already been added.",
"ExceptionType": "System.ArgumentException",
"StackTrace": " at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)\r\n at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)\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.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)"
}
答案 0 :(得分:1)
这个答案总结了聊天中发生的事情。所以它读取的触摸与标准答案不同。
我没有具体说明这个错误,但看起来OData很难找到“id”属性。将oData拉出到独立包后,会发生一些变化。
<强>版本吗
我要检查的第一件事是你是否有独立的oData包(Microsoft.Data.OData 5.0.1
和Microsoft.AspNet.WebApi.OData 0.1.0-alpha-120815
)。您可以通过在包管理控制台中键入Get-Package
来执行此操作。
在聊天中你发布了这些:
Microsoft.AspNet.WebApi.OData 0.1.0-alpha-120815
Microsoft.Data.Edm 5.1.0-rc2
Microsoft.Data.OData 5.1.0-rc2
Microsoft.Data.OData.Contrib 5.1.0.50918-rc
System.Spatial 5.1.0-rc2
所以看起来你还在运行产品的RC。 is an old bug似乎描述了这种行为。缺点是,具有继承属性的类会抛出错误。
当前不这样做
更新到当前版本似乎无法解决问题。所以下一步是尝试新签名的夜宵。
获取已签名的每晚构建are here的说明。
签名的夜间版本解决了这个问题。
另一种想法
oData更改后,产品需要能够识别关键属性。 This article个州
不知何故,[Queryable]属性必须找到一个键属性。这个 如果您的元素类型具有ID属性,则会自动发生 您可能需要手动配置模型(请参阅设置您的 模型)。
如果您正在使用Entity Framework,那么person对象是否定义了实体键?如果没有,您可以尝试“设置模型部分”中指定的oDataConventionModelBuilder
。类似的东西:
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
var pessoa= modelBuilder.EntitySet<Pessoa>("Pessoa");
pessoa..HasKey(p => p.Id);
...
答案 1 :(得分:0)
OData需要您为Pessoa类指定主键的概念。要么通过添加属性来执行此操作:
[DataServiceKey("Id")]
public class Pessoa : IEntity, IAuditable
{
public int Id { get; set; }
//...
}
或明确构建指定密钥的模型,如OData Web API发布博客文章http://blogs.msdn.com/b/alexj/archive/2012/08/15/odata-support-in-asp-net-web-api.aspx中的“设置模型”标题部分所述。