Because I'm supporting soft deletes in my database, I've chosen to sub-type my Thing
entity as ActiveThing
and 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"));
}
Now, my OData endpoint (which formerly exposed Thing
).. how do I get it to now only expose ActiveThing
s?
I think I figured it out..
Formerly, my dbContext model looked like this, only exposing my base class:
public class myDbModel:DbContext
{
public myDbModel(): base("name=ThingDb"){}
public DbSet<Thing> Things { get; set; } //db table + ThingsController source
}
Now, I've added add'l DbSets to expose my subtypes.. like this:
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; }
}
Here's my updated 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;
}
}