I have a base class and two derived classes.
Each of the derived classes implements the same type as the property - the only difference is the name of the property.
Unfortunately, I do not greatly influence the design of the class - → they were created from the wsdl file.
Then I have a BaseType property to encapsulate a common property. The plan was to use this property in my web views, etc.
I used the famous "Fruit Example" to demonstrate the problem:
public class FruitBase
{
public virtual int ID { get; set; }
[NotMapped]
public virtual FruitnessFactor Fruitness
{
get
{
if (this.GetType().BaseType == typeof(Apple))
return ((Apple)this).AppleFruitness;
else if (this.GetType().BaseType == typeof(Orange))
return ((Orange)this).OrangeFruitness;
else
return null;
}
}
}
public class FruitnessFactor { }
In my MVC controller, the following query works absolutely fine:
return View(context.FruitEntities
.OfType<Apple>().Include(a =>a.AppleFruitness)
.ToList());
But this is not:
return View(context.FruitEntities
.OfType<Apple>().Include(a =>a.AppleFruitness)
.OfType<Orange>().Include(o => o.OrangeFruitness)
.ToList());
The error message I get is:
DbOfTypeExpression requires an expression argument with a polymorphic result type that is compatible with the type argument.
EF 5.0 RC Code First.
!