【问题标题】:ServiceStack AutoQuery not working for DateTime valuesServiceStack AutoQuery 不适用于 DateTime 值
【发布时间】:2015-03-20 04:18:30
【问题描述】:

我有一个使用自动查询的 ServiceStack 服务,其中忽略大于或小于的 DateTime。

这是我的请求 DTO:

    public class GetSources : QueryBase<DbSource, Source>
    {
       public string Name { get; set; }
       public string NameContains { get; set; }
       public string NameStartsWith { get; set; }
       public DateTime? LastUpdatedDateGreaterThan { get; set; }
       public DateTime? LastUpdatedDateLessThan { get; set; }
    }

ormlite T4模板生成的数据库表poco如下所示:

[Alias("DbSources")]
[Schema("SomeSchema")]
public partial class DbSource 
{
    [AutoIncrement]
    public int Id { get; set;}
    [Required]
    public string Name { get; set;}
    [Required]
    public DateTime LastUpdatedDate { get; set;}
}

在服务中我做了一些验证,然后像这样使用 AutoQuery:

    var q = AutoQuery.CreateQuery(dto, Request.GetRequestParams());
    q.Join<DbSource, CompanySource>((source, companySource) => source.Id == companySource.SourceId && companySource.CompanyID == companyId);
    return AutoQuery.Execute(dto, q);

我正在使用 mstest

    [TestMethod]
    public void GetSources_LastUpdatedGreaterThan()
    {
        var expected = DateTime.Now;
        var query = new GetSources { LastUpdatedDateGreaterThan = expected};
        QueryResponse<Source> result;
        using (var service = appHost.Container.Resolve<SourceService>())
        {
            service.Request = new MockHttpRequest();
            result = service.Any(query);
        }
        log.Info(result.ToJson());
        result.Results.ForEach(src => Assert.IsTrue(src.LastUpdatedDate > expected));
    }

Name、NameContains 和 NameStartsWith 在其他测试中都按预期工作,但 LastUpdatedDateGreaterThan 和 LastUpdatedDateLessThan 都不会生成 where 子句。在我的 AutoQuery 设置中,所有属性都是默认值,但 EnableUntypedQueries 为 false。

我知道我可以在服务中明确添加它们的位置。即

    q.Where(source => source.LastUpdatedDate > dto.LastUpdatedDateGreaterThan);

但如果可能的话,我希望 AutoQuery 能够处理它。 DateTime 是否适用于 AutoQuery?还是我在代码中做错了什么。

【问题讨论】:

标签: servicestack ormlite-servicestack


【解决方案1】:

我运行了 myz 使用 sql server 创建的 Servicestack AutoQuery 单元测试。我使用原始项目正在查询的数据库视图进行了测试,但我无法复制我遇到的问题。现在,我将只将 QueryField 属性添加到 Query 模型上的 DateTime 属性中,如下所示:

[QueryField(Template = "{Field} > {Value}", Field = "LastUpdatedDate")]
public DateTime? LastUpdatedDateGreaterThan { get; set; }

添加属性给了我我需要的东西。稍后我会花一些时间追查代码中的罪魁祸首。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2014-05-13
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多