代码之家  ›  专栏  ›  技术社区  ›  Egil Hansen

ADO.NET数据服务:如何基于外键筛选实体?

  •  1
  • Egil Hansen  · 技术社区  · 16 年前

    这可能是一个与实体框架相关的问题,无论如何,这里是:

    考虑以下简单的数据库模式:

    CREATE TABLE Supplier(
        SupplierId int identity(1,1) not null primary key,
        DisplayName nvarchar(50)
    )
    
    CREATE TABLE Category(
        CategoryId int identity(1,1) not null primary key,
        DisplayName nvarchar(50)
    )
    
    CREATE TABLE Product(
        ProductId int identity(1,1) not null primary key,
        SupplierId int references Supplier.SupplierId,
        CategoryId int references Category.CategoryId,
        DisplayName nvarchar(50)
    )
    

    我想要的是根据供应商和类别筛选产品。通常我只提供一个类别ID和一个供应商ID,但是由于我通过EF公开我的数据库,数据服务不允许我这样做:

    $filter=SupplierId eq 1 and CategoryId eq 2
    

    这似乎是一个很常见的场景,所以这是可能的。但是如何呢?

    1 回复  |  直到 12 年前
        1
  •  2
  •   Egil Hansen    16 年前

    事实证明,这很容易,具体操作如下:

    $filter=Product/Supplier/SupplierID eq 1 and Products/Category/CategoryID eq 1
    

    当做。