0

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 ActiveThings?

bkwdesign
  • 1,953
  • 2
  • 28
  • 50

1 Answers1

0

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;
    }
}
bkwdesign
  • 1,953
  • 2
  • 28
  • 50
  • 1
    may be you will consider to use the `db` as a constructor arguement to ease injection and scope control... BTW: exposing directly the DbSet (even as a IQueryable) may have side effects – tschmit007 Apr 26 '17 at 16:56
  • fairly new app, not in production yet. definitely want to get it all unit-test-ified.. so, very good points to consider @tschmit007. Thanks. – bkwdesign Apr 26 '17 at 17:04