namespace MyOffice.Data.Repositories; using Microsoft.EntityFrameworkCore; using DbContext; public class RepositoryBase where TEntity : class { protected readonly AppDbContext _context; protected RepositoryBase( AppDbContext context ) { _context = context; } protected async Task SaveChangesAsync() { try { return await _context.SaveChangesAsync(); } catch (DbUpdateException updateException) { throw updateException; } } protected int SaveChanges() { try { return _context.SaveChanges(); } catch (DbUpdateException updateException) { throw updateException; } } protected async Task GetAsync(Guid id) { return await _context.Set().FindAsync(id); } protected async Task AddAsync(TEntity entity) { await _context.Set().AddAsync(entity); return await SaveChangesAsync(); } protected int Add(TEntity entity) { _context.Set().Add(entity); return SaveChanges(); } protected int Remove(TEntity entity) { _context.Set().Remove(entity); return SaveChanges(); } protected async Task UpdateAsync(TEntity entity) { _context.Attach(entity); _context.Entry(entity).State = EntityState.Modified; return await SaveChangesAsync(); } protected int Update(TEntity entity) { _context.Attach(entity); return SaveChanges(); } }