I use: EntityFramework + POCO
Here is the thing:
public interface IBaseType
{
int Id { get; set; }
}
public class BaseType : IBaseType
{
public virtual int Id { get; set; }
}
public class DerivedType : BaseType
{
}
The problem:
public class EntityFetcher<T> where T : BaseType
{
public object GetById(int id)
{
ObjectSet<T> objectSet = (ObjectSet<T>)GetTheObjectSet(typeof(T));
return objectSet.SingleOrDefault((o) => o.Id == id);
}
}
If T
is BaseType
this all works perfectly, but:
The problem is that in EntityFramework when one class inherits another they share the ObjectSet
and, therefore, if T
is DerivedType
then the GetTheObjectSet
returns ObjectSet<BaseType>
, which cannot be cast to ObjectSet<DerivedType>
.
Is there a way to actually cast this this thing or somehow else execute the SingleOrDefault
? Can those things be cast using the IObjectSet<>
and IBaseType
?