因为我支持数据库中的软删除,所以我选择将我的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?
发布于 2017-04-26 15:47:47
我想我想明白了..。
以前,我的dbContext模型是这样的,只公开了我的基类:
public class myDbModel:DbContext
{
public myDbModel(): base("name=ThingDb"){}
public DbSet<Thing> Things { get; set; } //db table + ThingsController source
}现在,我添加了‘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;
}
}https://stackoverflow.com/questions/43638171
复制相似问题