0

I have a type that is derived from an entity:

public class WidgetB : WidgetA 
{

}

WidgetA is a POCO object and exists in the EDM; WidgetB does not exist in the EDM but has been setup in the ObjectContext thru an interface like so:

public interface IContext
{
    IObjectSet<WidgetA> WidgetAs { get; }
    IQueryable<WidgetB> WidgetBs { get; }
}

public class CustomObjectContext : ObjectContext, IContext
{
    private IObjectSet<WidgetA> _widgetAs;
    public IObjectSet<WidgetA> WidgetAs
    {
        get { return _widgetAs ?? (_widgetAs = CreateObjectSet<WidgetAs>()); }
    }

    private IQueryable<WidgetB> _widgetBs;
    public IQueryable<WidgetB> WidgetBs
    {
        get { return _widgetBs ?? (_widgetBs = CreateObjectSet<WidgetA>("WidgetAs").OfType<WidgetB>()); }
    }

WidgetA has been setup as a ComplexType in the EDM with all the properties that WidgetA would have.

However when I make a call to the context interface:

    public WidgetB GetById(int id)
    {
        return _context.WidgetB.Include("Blah1").Include("Blah2").Where(r => r.Id == id).SingleOrDefault();
    }

this results in an error:

The 'OFTYPE expression' type argument must specify an EntityType. The passed type is ComplexType 'EntityModel.WidgetB'.

Any help would be appreciated.

Rich
  • 1,895
  • 3
  • 26
  • 35

1 Answers1

0

It is not possible. Context can materialize only entity types which are mapped in EDM. Your new class derived from entity type is not entity and cannot be handled by your EF context. The only way in this case is to use mapped entity inheritance.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • is this a good walk through for mapped entity inheritance? [Mapping Inheritance](http://msdn.microsoft.com/en-us/library/cc716702.aspx) – Rich Dec 15 '11 at 20:34
  • It is valid walk through for one of way to map the inheritance ([here](http://stackoverflow.com/questions/5439273/how-to-map-table-per-type-tpt-inheritance-in-entity-designer/5443033#5443033) you have compacted form). The question is if you want to use TPT inheritance where each type in inheritance tree has separate table. How many widgets types do you have? If you have only two you can probably use TPH inheritance which maps both widgets to the same table and has much better performance. – Ladislav Mrnka Dec 15 '11 at 20:49
  • In my scenario I only have two. At this point, it seems like the easiest solution will be to just wrap entity types instead of inheriting; this works cause I'm only reading the data. Thanks for the quick response! I'll look into TPT and TPH a little more. – Rich Dec 15 '11 at 20:57