将 TPH 与 Entity Framework 一起使用时,如何使我的 OData 仅公开特定类型?
When using TPH with Entity Framework, how do I make my OData only expose a particular type?
因为我在我的数据库中支持软删除,所以我选择将我的 Thing
实体子类型化为 ActiveThing
和 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
)..我如何让它现在只公开 ActiveThing
s?
我想我明白了..
以前,我的 dbContext 模型是这样的,只暴露了我的基础 class:
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;
}
}
因为我在我的数据库中支持软删除,所以我选择将我的 Thing
实体子类型化为 ActiveThing
和 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
)..我如何让它现在只公开 ActiveThing
s?
我想我明白了..
以前,我的 dbContext 模型是这样的,只暴露了我的基础 class:
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;
}
}