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>();
          }
        }      
    }
}

Update model from database and generate model from database causes "Object reference not set to an instance of an object"

I have recently experienced that I suddenly could not update my model from database, or generate a new model with the Entity Framework designer.

The generate dialog would simply not appear. So I created an empty model instead, and tried to update from database, and I got the error: ""Object reference not set to an instance of an object".

After reinstalling Visual Studio and SP1 without luck, I found the solution:

  1. 1) Open the "Visual Studio 2008 Command Prompt" (with administrator privileges if Vista)
  2. 2) devenv /resetskippkgs
  3. 3) Close Visual Studio
  4. 4) devenv /setup
  5. 5) Run Visual Studio

It forces Visual Studio to merge the resource metadata that describes menus, toolbars, and command groups from all VSPackages available, and I was up and running again.