Files
Backend-Api/0_Framework/InfraStructure/RepositoryBase.cs
2024-07-05 21:36:15 +03:30

60 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using _0_Framework.Domain;
using Microsoft.EntityFrameworkCore;
namespace _0_Framework.InfraStructure
{
public class RepositoryBase<TKey, T> : IRepository<TKey, T> where T:class
{
private readonly DbContext _context;
public RepositoryBase(DbContext context)
{
_context = context;
}
public void Create(T entity)
{
_context.Add(entity);
}
public bool Exists(Expression<Func<T, bool>> expression)
{
return _context.Set<T>().Any(expression);
}
public T Get(TKey id)
{
return _context.Find<T>(id);
}
public List<T> Get()
{
return _context.Set<T>().ToList();
}
public void Remove(T entity)
{
_context.Set<T>().Remove(entity);
}
public void RemoveRange(IEnumerable<T> entities)
{
_context.Set<T>().RemoveRange(entities);
}
public void SaveChanges()
{
_context.SaveChanges();
}
public async Task SaveChangesAsync()
{
await _context.SaveChangesAsync();
}
}
}