I have a generic repository an I am trying to add a GetById method as shown here
C# LINQ to SQL: Refactoring this Generic GetByID method
The problem is my repository does not use System.Data.Linq.DataContext
instead I use System.Data.Entity.DbContext
So I get errors where I try to use
Mapping.GetMetaType
and
return _set.Where( whereExpression).Single();
How can I implement a generic GetById method in CTP5? Should I be using System.Data.Entity.DbContext in my Repository.
Here is the start of my repository class
public class BaseRepository<T> where T : class
{
private DbContext _context;
private readonly DbSet<T> _set;
public BaseRepository()
{
_context = new MyDBContext();
_set = _context.Set<T>();
}
The most basic approach is simply
If you know that all your entities have primary key called Id (it doesn’t have to be called Id in DB but it must be mapped to property Id) of defined type you can use simply this:
If data type is not always the same you can use:
You can also simply use: