Search code examples
c#entity-framework-6table-per-hierarchy

Entity Framework:- Error when Casting to derive class throw exception in Table per hierarchy query


I faced an exception when i try to cast to derived class; Unable to cast the type '' to type ''. LINQ to Entities only supports casting EDM primitive or enumeration types.

 (obj => ((DerivedClass)obj).DerivedProperty == true);

Solution

  • Thanks all for your help; I solved the problem by using the as operator instead of direct casting; I don't know the reason but this fixed my issue.

    obj => (obj as DerivedClass).DerivedProperty == true;