51 lines
1.3 KiB
C#
51 lines
1.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BelegeingangDatabase.Repositories
|
|
{
|
|
public class GenericRepository<T> : IGenericRepository<T> where T : class
|
|
{
|
|
protected readonly BelegeingangContext _context;
|
|
private readonly DbSet<T> _dbSet;
|
|
|
|
public GenericRepository(BelegeingangContext context)
|
|
{
|
|
_context = context;
|
|
_dbSet = _context.Set<T>();
|
|
}
|
|
|
|
public virtual async Task<IEnumerable<T>> GetAllAsync()
|
|
{
|
|
return await _dbSet.ToListAsync();
|
|
}
|
|
|
|
public virtual async Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate)
|
|
{
|
|
return await _dbSet.Where(predicate).ToListAsync();
|
|
}
|
|
|
|
public virtual async Task<T> GetByIdAsync(int id)
|
|
{
|
|
return await _dbSet.FindAsync(id);
|
|
}
|
|
|
|
public virtual async Task AddAsync(T entity)
|
|
{
|
|
await _dbSet.AddAsync(entity);
|
|
}
|
|
|
|
public virtual void Update(T entity)
|
|
{
|
|
_dbSet.Update(entity);
|
|
}
|
|
|
|
public virtual void Remove(T entity)
|
|
{
|
|
_dbSet.Remove(entity);
|
|
}
|
|
}
|
|
} |