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.
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.