Servicestack AutoQuery 不过滤结果

Servicestack AutoQuery not filtering results

我的查询 /API/Json/GetJson?Desc=test1

我得到了所有记录,而不仅仅是 test1 记录

[Route("/API/Json/GetJson", "GET")]
public class GetJson : QueryDb<JsonModel>
{
    public int? Id { get; set; }
    public int? RefId { get; set; }
    public int? SecondRefId { get; set; }
    public int? ThirdRefId { get; set; }
    public int? FourthRefId { get; set; }
    public string Name { get; set; }
    public string JSON { get; set; }
    public string JsonType { get; set; }
    public string Desc { get; set; }
    public int? AuditId { get; set; }
}

public class JsonModel
{
    [AutoIncrement]
    [PrimaryKey]
    [IgnoreOnUpdate]
    public int Id { get; set; }
    /// <summary>
    /// Other tables the this data is relevant to
    /// </summary>
    public int? RefId { get; set; }
    public int? SecondRefId { get; set; }
    public int? ThirdRefId { get; set; }
    public int? FourthRefId { get; set; }

    /// <summary>
    /// name that is displayed to users
    /// </summary>
    [Required]
    public string Name { get; set; }

    public string JSON { get; set; }
    /// <summary>
    /// Tells what data type the JSON is storing
    /// </summary>
    [Required]
    public string JsonType { get; set; }

    public string Desc { get; set; }

    public int AuditId { get; set; }

    public DateTime AuditStamp { get; set; } = DateTime.UtcNow;
}

我的 return 数据也有额外的字段。从 Skip

开始
{
    "id": 4,
    "refId": 9,
    "secondRefId": 3,
    "thirdRefId": 100,
    "fourthRefId": null,
    "name": "test",
    "json": "JSON STRING DATA",
    "jsonType": "test",
    "desc": "test3",
    "auditId": 0,
**"skip": null,
"take": null,
"orderBy": null,
"orderByDesc": null,
"include": null,
"fields": null,
"meta": null**
},

我将我的模型更新为可空值,直到返回所有记录。我的种子数据和我使用的是 SS 5.6.0

WireUpService<IntegrationService>();

    using (var db = HostContext.Resolve<IDbConnectionFactory>().Open())
    {
        string JSON = " \"Columns\": [\r\n      {\r\n        \"CompanyName\": [\r\n          {\r\n            \"name\": \"Company Name\",\r\n            \"Visible\": \"True\",\r\n            \"Sort\": \"U,A,Z[Unsorted, A-Z, Z-A]\",\r\n            \"Filter\": \"Test Company\"\r\n          }\r\n        ],\r\n        \"ParentCompnay\": [\r\n          {\r\n            \"name\": \"Company Name\",\r\n            \"Visible\": \"True\",\r\n            \"Sort\": \"U,A,Z[Unsorted, A-Z, Z-A]\",\r\n            \"Filter\": \"Test Company\"\r\n          }\r\n        ]\r\n      }";
        db.DropAndCreateTable<JsonModel>();
        db.Insert(new JsonModel { Desc = "test",Name = "test",JsonType = "test", JSON = JSON,RefId = 10,SecondRefId = 3, AuditId = 0, AuditStamp = DateTime.Now });
        db.Insert(new JsonModel { Desc = "test1", Name = "test", JsonType = "test", JSON = JSON, RefId = 10, SecondRefId = 3, AuditId = 0, AuditStamp = DateTime.Now });
        db.Insert(new JsonModel { Desc = "test2", Name = "test", JsonType = "test", JSON = JSON, RefId = 5, SecondRefId = 3, AuditId = 0, AuditStamp = DateTime.Now });
        db.Insert(new JsonModel { Desc = "test3", Name = "test", JsonType = "test", JSON = JSON, RefId = 9, SecondRefId = 3,ThirdRefId = 100, AuditId = 0, AuditStamp = DateTime.Now });
    }

我无法使用提供的 类 重现此问题,我已经用 匹配查询 和 [=33= 的测试数据播种了该问题]您在 JSON 响应中包含的不匹配测试数据:

db.CreateTable<JsonModel>();
db.Insert(new JsonModel { RefId = 1, SecondRefId = 1, ThirdRefId = 111, Name = "test1", Desc = "test1", JsonType = "test", JSON = "TEST1"});
db.Insert(new JsonModel { RefId = 9, SecondRefId = 3, ThirdRefId = 100, Name = "test1", Desc = "test3", JsonType = "test", JSON = "JSON STRING DATA"});

您的 GetJson 模型存在问题,您将 IdAuditId 指定为不可为 null 的 int 属性,如果未指定,这些属性将填充到您的 GetJson 请求 DTO 作为 0 (默认整数)。

如果您要在 GetJson AutoQuery 服务中包含所需的值类型,您应该始终为它们提供值,否则将它们更改为 int?,这样它们就不会添加到查询中未指定时进行过滤,例如:

public class GetJson : QueryDb<JsonModel>
{
    public int? Id { get; set; }
    public int? AuditId { get; set; }
}

执行此操作后,它按预期工作,我可以使用您指定的查询查询自动查询服务,即:

var url = baseUrl.CombineWith("/API/Json/GetJson").AddQueryParam("Desc", "test1");
var json = url.GetJsonFromUrl();
json.Print();

按预期工作,在 QueryResponse<T> DTO 中返回匹配结果,即:

{"Offset":0,"Total":0,"Results":[{"Id":1,"RefId":1,"SecondRefId":1,"ThirdRefId":111,"Name":"test1","JSON":"TEST1","JsonType":"test","Desc":"test1","AuditId":0,"AuditStamp":"\/Date(1577190306230-0000)\/"}],"Meta":{}}