Entity Framework: Property for derived entities on the ObjectContext

The first thing you probably wonder when you create an inherited structure in the Entity Framework is, that the ObjectContext doesn't contain a property for your derived entities - this is a little confusing.

This is because there is only one EntitySet, and there is only generated one property pr. EntitySet.

To make things more intuitive, you can add the property yourself, as the ObjectContext is partial. If we have the following:

image

You only have a PersonSet and a Person property. To get properties for Employee and Customer, you just add a partial class to your objectcontext and add the properties like so:

namespace Devtalk
{
    public partial class TestEntities
    {
        public IQueryable<Employee> Employee
        {
          get
          {
            return Person.OfType<Employee>();
          }
        }

        public IQueryable<Customer> Customer
        {
          get
          {
            return Person.OfType<Customer>();
          }
        }      
    }
}
All comments require the approval of the site owner before being displayed.
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview