EF4: common interface for EF entities

Posted by Feryt on Stack Overflow See other posts from Stack Overflow or by Feryt
Published on 2010-05-28T15:40:47Z Indexed on 2010/05/28 15:41 UTC
Read the original article Hit count: 258

Filed under:

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.

© Stack Overflow or respective owner

Related posts about entity-framework-4