0

因为我在我的数据库中支持软删除,所以我选择将我的Thing实体子类型为ActiveThingand DeletedThing...

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // TPH (table-per-hierarchy):
    modelBuilder.Entity<MyCorp.Biz.CoolApp.Thing>()
            .Map<MyCorp.Biz.CoolApp.ActiveThing>(thg => thg.Requires("Discriminator").HasValue("A"))
            .Map<MyCorp.Biz.CoolApp.DeletedThing>(thg => thg.Requires("Discriminator").HasValue("D"));
}

现在,我的 OData 端点(以前暴露Thing)..我如何让它现在只暴露ActiveThings?

4

1 回答 1

0

我想我想通了。。

以前,我的 dbContext 模型看起来像这样,只暴露了我的基类:

public class myDbModel:DbContext
{
    public myDbModel(): base("name=ThingDb"){}

    public DbSet<Thing> Things { get; set; }  //db table + ThingsController source

}

现在,我添加了 add'l DbSets 来公开我的子类型.. 像这样:

public class myDbModel:DbContext
{
    public myDbModel(): base("name=ThingDb"){}

    public DbSet<Thing> Things { get; set; }  //db table

    public DbSet<ActiveThing> ActiveThings { get; set; } // now my ThingsController 'GetThings' pulls from this

    public DbSet<DeletedThing> DeletedThings { get; set; }
}

这是我更新的 ThingsController.cs

public class ThingsController : ODataController
{
    private myDbModel db = new myDbModel();

    /// <summary>
    /// Only exposes ActiveThings (not DeletedThings)
    /// </summary>
    /// <returns></returns>
    [EnableQuery]
    public IQueryable<Thing> GetThings()
    {
        return db.ActiveThings;
    }
}
于 2017-04-26T15:47:47.537 回答