Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a generic GetById method using Entity Framework 4 and the Repository Pattern?

I have a generic repository and would like to implement a generic GetById method. This is my repository interface so far:

public interface IRepository<T> where T : EntityObject  
{
    void Add(T entity);
    void Delete(int id);
    void Delete(T entity);
    IEnumerable<T> Find(Expression<Func<T, bool>> predicate);
    T SingleOrDefault(Expression<Func<T, bool>> predicate);
    IEnumerable<T> GetAll();
    T GetById(int id);
    void Save();
    T Single(Expression<Func<T, bool>> predicate);
}

I am using the Entity Framework 4.1. Lots of the solutions I found were using an abstract base class of interface for an IEntity class which the entities have to inherit from in order to perform the Id lookup.

Instead of having to implement an interface for all my classes I was trying to use this bit of code:

T entity = _objectSet.Where(
    x => x.EntityKey.EntityKeyValues
          .Select(v => v.Value.Equals(id)).Count() == 1).First();

However when trying to use the method I receive an exception:

The specified type member 'EntityKey' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

Could someone tell me how I can get this to work?

Update 1

The members _objectContext and _context are declared like this:

private readonly ObjectContext _context;
private readonly IObjectSet<T> _objectSet;

Thanks.

like image 285
b3n Avatar asked Apr 03 '11 03:04

b3n


1 Answers

From this question How to get ObjectSet<T>'s entity key name?, you can cheat a little and use ESQL.

    public T GetById(int id)
    {

        var keyPropertyName = _objectSet.EntitySet.ElementType.KeyMembers[0].ToString();        

        T entity = _objectSet.Where("it." + keyPropertyName + "=" + id).First();
        return entity;

    }

I've included all the needed code in the method, obviously you want to refactor it a little, but it should work for you.

like image 183
Rob Avatar answered Nov 15 '22 09:11

Rob