开发者

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

开发者 https://www.devze.com 2023-02-21 10:42 出处:网络
I have a generic repository and would like to implement a generic GetById method. This is my repository interface so far:

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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消