I have an extension method for a common interface used on my entities
public static IQueryable<T> IsEdited<T>(this IQueryable<T> source)
where T : IAuditData
{
return from o in source
where o.CreatedOn != o.UpdatedOn
select o;
}
when I call it
var editedUsers = dataContext.Users.IsEdited();
I get
Unable to cast the type ‘IUserData’ to type
‘IAuditData’. LINQ to Entities only
supports casting Entity Data Model primitive types
IUserData is an interface too, I use it to abstract the DbContext’s DbSet properties
IQueryable<IUserData> Users { get { ... } }
but if I add a class constraint, all is well
public static IQueryable<T> IsEdited<T>(this IQueryable<T> source)
where T : class, IAuditData
{
return from o in source
where o.CreatedOn != o.UpdatedOn
select o;
}
which makes me happy.
But what it is that the compiler is doing to tell EF that T is a reference type?
structtype are not supported by EF. If you do not constraint the parameter as a class it can also be a struct.