12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using Azure;
- using Microsoft.EntityFrameworkCore;
- using MTWorkHR.Core.Entities.Base;
- using MTWorkHR.Core.IRepositories.Base;
- using MTWorkHR.Infrastructure.Data;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MTWorkHR.Infrastructure.Repositories
- {
- public class Repository<T> : IRepository<T> where T : Entity
- {
- protected readonly HRDataContext _context;
- private readonly DbSet<T> dbSet;
- public Repository(HRDataContext context)
- {
- _context = context;
- dbSet = context.Set<T>();
- }
- public async Task<T> AddAsync(T entity)
- {
- await _context.AddAsync(entity);
- await _context.SaveChangesAsync();
- return entity;
- }
- public async Task<IList<T>> AddRangeAsync(IList<T> entities)
- {
- await dbSet.AddRangeAsync(entities);
- return entities;
- }
- public async Task DeleteAsync(T entity)
- {
- dbSet.Remove(entity);
- }
- public async Task DeleteRangeAsync(IEnumerable<T> entities)
- {
- dbSet.RemoveRange(entities);
- }
- public async Task<Tuple<ICollection<T>, int>> GetAllAsync()
- {
- var query = dbSet.AsQueryable();
- var total = await query.CountAsync();
- return new Tuple<ICollection<T>, int>(await query.ToListAsync(), total);
- }
- public async Task<T> GetByIdAsync(long id)
- {
- return await _context.Set<T>().FindAsync(id);
- }
- }
- }
|