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 GetBaseAsync(Guid id) { return await _context.Set().FindAsync(id); } protected async Task AddBaseAsync(TEntity entity) { //await _context.Set().AddAsync(entity); await _context.AddAsync(entity); return await SaveChangesAsync(); } protected int AddBase(TEntity entity) { //_context.Set().Add(entity); _context.Add(entity); return SaveChanges(); } protected int RemoveBase(TEntity entity) { _context.Set().Remove(entity); _context.Entry(entity).State = EntityState.Deleted; return SaveChanges(); } protected async Task UpdateBaseAsync(TEntity entity) { _context.Attach(entity); _context.Entry(entity).State = EntityState.Modified; return await SaveChangesAsync(); } protected int UpdateBase(TEntity entity) { _context.Attach(entity); _context.Entry(entity).State = EntityState.Modified; return SaveChanges(); } }