我在 URI 中使用的任何 OData 命令都会显示以下错误!
当我使用没有任何配置/参数 OData 的 api 时,效果很好!
但是 any$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));
}
[添加] 处理类
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)"
}
佩索阿类
[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)"
}