OrmLite不会对联接应用软删除过滤器

时间:2018-09-05 17:21:02

标签: c# ormlite-servicestack

我在ServiceStack OrmLite中添加了SqlExpressionSelectFilter以使用soft delete功能。但是,似乎仅将过滤器应用于From<T>中指定的基表,而不应用于与Join<TFrom, TOther>联接的任何表。我的两种类型都实现了我在过滤器中使用的接口。这是受支持的吗?

过滤器:

OrmLiteConfig.SqlExpressionSelectFilter = query =>
{
    if (query.ModelDef.ModelType.HasInterface(typeof(IHasRecordStatus)))
    {
        query.Where<IHasRecordStatus>(q => q.RecordStatus != RecordStatus.Deleted);
    }
};

接口:

public interface IHasRecordStatus
{
    [Alias("RECORD_STATUS")]
    RecordStatus RecordStatus { get; set; }
}

[EnumAsInt]
public enum RecordStatus : byte
{
    Added = 1,
    Changed = 2,
    Deleted = 3
}

POCO:

[Alias("ITEM_MASTER")]
public class ItemMaster : IHasRecordStatus
{
    [PrimaryKey]
    [Alias("ITEM_ID")]
    public int Id { get; set; }

    [Alias("UPC_EAN")]
    public string Upc { get; set; }

    [Alias("RECORD_STATUS")]
    public RecordStatus RecordStatus { get; set; }
}

[Alias("VENDOR_ITEM")]
public class VendorItem : IHasRecordStatus
{
    [Alias("VI_ID")]
    public int Id { get; set; }

    [Alias("ITEM_ID")]
    public int ItemId { get; set; }

    [Alias("V_ID")]
    public int VendorId { get; set; }

    [Alias("RECORD_STATUS")]
    public RecordStatus RecordStatus { get; set; }
}

查询:

var query = db.From<VendorItem>()
              .Join<VendorItem, ItemMaster>((vi, im) => vi.ItemId == im.Id)
              .Take(2);

var result = db.Select(query);

输出SQL:

SELECT TOP 2 "VENDOR_ITEM"."VI_ID" AS "Id", "VENDOR_ITEM"."ITEM_ID" AS "ItemId", "VENDOR_ITEM"."V_ID" AS "VendorId", "VENDOR_ITEM"."RECORD_STATUS" AS "RecordStatus" 
FROM "VENDOR_ITEM" INNER JOIN "ITEM_MASTER" ON ("VENDOR_ITEM"."ITEM_ID" = "ITEM_MASTER"."ITEM_ID")
WHERE ("VENDOR_ITEM"."RECORD_STATUS" <> @0)

在生成的SQL中,我希望where子句也包含(AND "ITEM_MASTER"."RECORD_STATUS" <> @1),但是它只包含from子句中指定的VendorItem表的过滤器。

我也发现奇怪的是,我必须在接口和实现它的类上都指定别名。我只在接口上和仅在POCO上尝试过它,直到我都指定了它们都没有用。这是设计使然吗?

1 个答案:

答案 0 :(得分:2)

OrmLite仅在from表上执行SqlExpressionSelectFilter,并且当您使用接口查询时,OrmLite将无法找到具体类型的元数据,这就是为什么需要在接口上指定元数据的原因。