我有一个使用自动查询的ServiceStack服务,其中大于或小于的日期时间被忽略。
这是我的要求 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;}
}
在服务中,我做了一些验证,然后像这样使用自动查询:
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 和 NameStarts在其他测试中按预期工作,但 LastUpdateDateGreaterThan 和 LastUpdateDateLessThan 都不会生成 where 子句。在我的自动查询设置中,所有属性都是默认值,除了 EnableUntypedQuery 它是假的。
我知道我可以在服务中明确添加它们的位置。
即 q.Where(source => source.LastUpdatedDate > dto.LastUpdatedDateGreaterThan);
但如果可能的话,我希望自动查询来处理它。 日期时间是否适用于自动查询?还是我在代码中做错了什么。
我浏览了mythz使用sql server创建的Servicestack AutoQuery单元测试。我使用原始项目正在查询的数据库视图进行了测试,但我无法复制我遇到的问题。现在,我将只将 QueryField 属性添加到查询模型的 DateTime 属性中,如下所示:
[QueryField(Template = "{Field} > {Value}", Field = "LastUpdatedDate")]
public DateTime? LastUpdatedDateGreaterThan { get; set; }
添加属性给了我需要的东西。 稍后我将花一些时间追踪代码中的罪魁祸首。