Generic access to DbContext
have a look at this tip that I posted a while ago
Anyway the trick is by using context.Set<>
public interface IEntity
{
int Id { get; set; }
}
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity
{
private IDbContext _context;
public Repository(IDbContext context)
{
_context = context;
}
private IDbSet<TEntity> DbSet
{
get
{
return _context.Set<TEntity>();
}
}
public IQueryable<TEntity> GetAll()
{
return DbSet.AsQueryable();
}
public void Delete(TEntity entity)
{
DbSet.Remove(entity);
}
.....
Related videos on Youtube
yairr
Recently used Angular, C# .NET Core, Java, JavaScript, Decision Trees for AI B.S. Computer Science. Microsoft Certified Professional.
Updated on June 04, 2022Comments
-
yairr almost 2 years
ObjectContext
allows generic access to the generated Entities.DbContext
appears to have no such support. Accessing EF5 with a generic repository is challenging. Let's say I want a generic mechanism to read any given entity, call it TEntity:public class DataRepositoryEF5<T> where T: DbContext { private ObjectContext _context; public DataRepositoryEF5(DbContext context) { _context = ((IObjectContextAdapter)context).ObjectContext; } public IEnumerable<TEntity> ReadAll<TEntity>() where TEntity : class,new() { return GetObjectSet<TEntity>().AsEnumerable(); } protected ObjectSet<TEntity> GetObjectSet<TEntity>() where TEntity : class,new() { ObjectSet<TEntity> result; result = _context.CreateObjectSet<TEntity>(); return result; } }
Usage
var context = new MyContext(); var repository = new DataRepositoryEF5<MyContext>(context); IEnumerable<Document> results = repository.GetAll<Document>(); foreach (var item in results) { Console.WriteLine("{0} {1} {2} {3}", item.Description, item.Id, item.Property, item.Whatever); }
EF used to generate classes with a common base type
EntityObject
. Since that is no longer the case the best constraint I can have isclass
...Second, because there is no equivalent of
ObjectContext.CreateObjectSet<>()
I am forced to cast fromDbSet
toObjectSet
.With this pattern in place I have no need for
DbContext
. Without generics I am forced to hand code all CRUD operations. Did I miss something? If not, is it possible to tell EF5 to generate code withObjectContext
instead?-
Massimiliano Peluso over 10 yearshave a look at this tip that I posted on codeproject a while ago codeproject.com/Tips/309753/…
-