首页 > 解决方案 > 带日期时间的 Odata V4

问题描述

嗨,我ODate在比较日期时遇到问题。当我执行下一个请求时:

https://localhost:44391/api/v1/Invoice/OData?$top=11&$skip=0&$filter=InvoiceDate lt 2014-10-30

我得到下一个错误:

{
    "StatusCode": 500,
    "Message": "Unable to cast object of type 'Microsoft.OData.UriParser.ConvertNode' to type 'Microsoft.OData.UriParser.ConstantNode'."
}

我在数据库中的日期如下:

2013-12-30 00:00:00.000
2013-12-30 00:00:00.000
2017-07-14 00:00:00.000

在我的课Startup上,我有下一个:

private static IEdmModel GetEdmModel()
{
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    List<string> listaControllers = GetControllers();
    foreach (string controller in listaControllers)
    {
        builder.EntitySet<BaseEntityDto>(controller);
    }
    return builder.GetEdmModel();
}

private static List<string> GetControllers()
{
    Type parentType = typeof(ApiControllerBase<,,>);
    Assembly assembly = Assembly.GetExecutingAssembly();
    return assembly.GetTypes().Where(x => x.BaseType.Name == parentType.Name).Select(x => x.Name.Replace("Controller", "")).ToList<string>();
}
app.UseMvc(routeBuilder =>
{
    routeBuilder.Expand().Select().Filter().Count().OrderBy().MaxTop(null);
    routeBuilder.EnableDependencyInjection();
    routeBuilder.MapODataServiceRoute("odata", "odata", GetEdmModel());
});

在我的实体中,Invoice.cs我有下一个属性:

public DateTime InvoiceDate { get; set; }

当我在 Postman 中执行最后一个查询时,执行的控制器 ( Invoicecontroller.cs) 是下一个:

[HttpGet("OData")]
public async Task<PageResult<InvoiceDto>> Get(ODataQueryOptions<InvoiceDto> queryOptions)
{
    var options = new ODataQueryListOptions(queryOptions);
    var result = await InvoiceQueryService.GetAll(options.QueryListOptions);
    var count = await InvoiceQueryService.Count(options.QueryListOptions);
    return new PageResult<InvoiceDto>(result, null, count);
}

我不知道问题出在哪里,我需要更多信息来解决问题。感谢大家试图解决这个问题。我想说我使用 Views no Tables。

标签: .net-coreodata

解决方案


只把它放在我的实体中解决我的问题:

[Column(TypeName = "datetime")]
public DateTime InvoiceDate { get; set; }

感谢所有社区阅读我的问题。


推荐阅读