EF4: common interface for EF entities
- by Feryt
Hi.
I have public interface:
public interface IEntity
{
int ID { get; set; }
string Name { get; set; }
bool IsEnabled { get; set; }
}
ehich some EF entities implements(thanks to partial class) and extesion method:
public static IEnumerable<SelectListItem> ToSelectListItems<T>(this IQueryable<T> entities, int? selectedID = null) where T : IEntity
{
return entities.Select(c => new { c.Name, c.ID }).ToList().Select(c => new SelectListItem { Text = c.Name, Value = c.ID.ToString(), Selected = (c.ID == selectedID) });
}
Calling ToSelectListItems return exception:
Unable to cast the type '<EF entity name>' to type 'IEntity'. LINQ to Entities only supports casting Entity Data Model primitive types.
Why, any ideas?
Thank you.