Does anyone have a very complete example generic repository for EF 6.1?
entity-framework, entity-framework-6
Solution
You can add new features like this:
public virtual void AddRange(IEnumerable<T> entities)
{
DbContext.Set<T>().AddRange(entities);
}
public virtual void RemoveRange(IEnumerable<T> entities)
{
DbContext.Set<T>().RemoveRange(entities);
}
Problem
I have my own repository that is as follows. However this does not take into account some of the new features such as the range features. Does anyone have a repository that includes everything. I have searched for this on the net but there's nothing that I can find that is recent. Here is what I have. I am hoping for something that has more and that offers IQueryable for many of the methods: ``` namespace Services.Repositories { /// <summary> /// The EF-dependent, generic repository for data access /// </summary> /// <typeparam name="T">Type of entity for this Repository.</typeparam> public class GenericRepository<T> : IRepository<T> where T : class { public GenericRepository(DbContext dbContext) { if (dbContext == null) throw new ArgumentNullException("An instance of DbContext is required to use this repository", "context"); DbContext = dbContext; DbSet = DbContext.Set<T>(); } protected DbContext DbContext { get; set; } protected DbSet<T> DbSet { get; set; } public virtual IQueryable<T> Find(Expression<Func<T, bool>> predicate) { return DbSet.Where<T>(predicate); } public virtual IQueryable<T> GetAll() { return DbSet; } public virtual T GetById(int id) { //return DbSet.FirstOrDefault(PredicateBuilder.GetByIdPredicate<T>(id)); return DbSet.Find(id); } public virtual void Add(T entity) { DbEntityEntry dbEntityEntry = DbContext.Entry(entity); if (dbEntityEntry.State != EntityState.Detached) { dbEntityEntry.State = EntityState.Added; } else { DbSet.Add(entity); } } public virtual void Update(T entity) { DbEntityEntry dbEntityEntry = DbContext.Entry(entity); if (dbEntityEntry.State == EntityState.Detached) { DbSet.Attach(entity); } dbEntityEntry.State = EntityState.Modified; } public virtual void Delete(T entity) { DbEntityEntry dbEntityEntry = DbContext.Entry(entity); if (dbEntityEntry.State != EntityState.Deleted) { dbEntityEntry.State = EntityState.Deleted; } else { DbSet.Attach(entity); DbSet.Remove(entity); } } public virtual void Delete(int id) { var entity = GetById(id); if (entity == null) return; // not found; assume already deleted. Delete(entity); } } } ```