b3n
b3n

Reputation: 3865

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.

Upvotes: 6

Views: 5749

Answers (2)

Atron Seige
Atron Seige

Reputation: 3049

For those who are "lucky" enough to work in VB.net and you are struggling to get a working example:

Imports System.Runtime.CompilerServices
Imports System.Data.Objects
Imports System.Collections.Generic

Public Module ObjectSetExtension

    <Extension()>
    Public Function GetById(Of T As Class)(self As ObjectSet(Of T), Id As Integer)
        Dim keyPropertyName = self.EntitySet.ElementType.KeyMembers(0).ToString()
        Dim entity As T = self.Where("it." & keyPropertyName & "=" & Id).First
        Return entity
    End Function

    <Extension()>
    Public Function GetById(Of T As Class)(self As ObjectSet(Of T), Id As Guid)
        Dim keyPropertyName = self.EntitySet.ElementType.KeyMembers(0).ToString()
        ' 'note the "GUID" in the string. DO NOT REMOVE!
        Dim entity As T = self.Where("it." & keyPropertyName & "=GUID '" & Id.ToString() & "'").First
        Return entity
    End Function

End Module

Upvotes: 0

Rob
Rob

Reputation: 1328

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.

Upvotes: 6

Related Questions