LINQ to entities seulement prend en charge la conversion EDM primitive ou une énumération des types avec IEntity interface

J'ai le générique suivant la méthode d'extension:

public static T GetById<T>(this IQueryable<T> collection, Guid id) 
    where T : IEntity
{
    Expression<Func<T, bool>> predicate = e => e.Id == id;

    T entity;

    //Allow reporting more descriptive error messages.
    try
    {
        entity = collection.SingleOrDefault(predicate);
    }
    catch (Exception ex)
    {
        throw new InvalidOperationException(string.Format(
            "There was an error retrieving an {0} with id {1}. {2}",
            typeof(T).Name, id, ex.Message), ex);
    }

    if (entity == null)
    {
        throw new KeyNotFoundException(string.Format(
            "{0} with id {1} was not found.",
            typeof(T).Name, id));
    }

    return entity;
}

Malheureusement Entity Framework ne sais pas comment gérer la predicate depuis C# converti le prédicat suivant:

e => ((IEntity)e).Id == id

Entity Framework lève l'exception suivante:

Impossible de convertir le type "IEntity' de type 'SomeEntity'. LINQ to
Seules entités prend en charge la conversion EDM primitive ou une énumération des types.

Comment pouvons-nous faire Entité Cadre de notre travail avec nos IEntity interface?

InformationsquelleAutor Steven | 2013-09-24