Files
Backend-Api/0_Framework/InfraStructure/RepositoryBase.cs
2025-08-09 14:45:04 +03:30

81 lines
1.9 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;
using Microsoft.EntityFrameworkCore.Storage;
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 AddRange(IEnumerable<T> entities)
{
_context.AddRange(entities);
}
public void Create(T entity)
{
_context.Add(entity);
}
public async Task CreateAsync(T entity)
{
await _context.AddAsync(entity);
}
public bool ExistsIgnoreQueryFilter(Expression<Func<T, bool>> expression)
{
return _context.Set<T>().IgnoreQueryFilters().Any(expression);
}
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();
}
public async Task<IDbContextTransaction> BeginTransactionAsync()
{
return await _context.Database.BeginTransactionAsync();
}
}
}