Skip to content

Instantly share code, notes, and snippets.

@JonathanLoscalzo
Created May 24, 2018 04:17
Show Gist options
  • Save JonathanLoscalzo/aea0d503b4d94a2cf9a39d35e49a1e81 to your computer and use it in GitHub Desktop.
Save JonathanLoscalzo/aea0d503b4d94a2cf9a39d35e49a1e81 to your computer and use it in GitHub Desktop.
public class EfRepository<T> : IRepository<T> where T : BaseEntity
{
private readonly AppDbContext _dbContext;
public EfRepository(AppDbContext dbContext)
{
_dbContext = dbContext;
}
public T GetById(int id)
{
return _dbContext.Set<T>().SingleOrDefault(e => e.Id == id);
}
public List<T> List()
{
return _dbContext.Set<T>().ToList();
}
public T Add(T entity)
{
_dbContext.Set<T>().Add(entity);
_dbContext.SaveChanges();
return entity;
}
public void Delete(T entity)
{
_dbContext.Set<T>().Remove(entity);
_dbContext.SaveChanges();
}
public void Update(T entity)
{
_dbContext.Entry(entity).State = EntityState.Modified;
_dbContext.SaveChanges();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment