views:

35

answers:

1

I have a Show table, and I would like to have a derived type called ActiveShow which only returns shows in the future

Show.ShowDateTime > DateTime.Now

Is there a way that I can achieve this using the designer or some other way so that creating an instance of ActiveShow will always adhere to the date condition?

+1  A: 

Absolutely you could do this using a DefiningQuery (which is essentially a TSQL view) in the SSDL.

But I don't recommend it.

The problem is type memberships would be transient, when it should be permanent, or at the very least require you to explicitly change it.

I.e. you could end up in a situation where at one point something is an ActiveShow (and loaded in memory) but if you do a subsequent query you might attempt to load the same object as a Show. In this situation what would happen to identity resolution is anyone's guess.

This will more than likely resort in some very nasty unexpected side-effects.

As an alternative perhaps an extra Property in your Context added in a partial class:

i.e.

public partial class MyContext
{
    public ObjectQuery<Show> ActiveShows
    {
       get{
           return this.Shows.Where(s => ShowDateTime > DateTime.Now) 
                  as ObjectQuery<Show>;
       }
    }
}

This probably gives you most of the benefits without most of the risks.

Hope this helps

Alex

Alex James