d254873172
- HrynCo.DAL.Abstract: IEntity<TId>, Entity base classes, ITransaction, IUnitOfWork - HrynCo.DAL.EF: EfRepository<TDbContext,TEntity>, EfUnitOfWork<TDbContext>, EfTransactionAdapter - Directory.Packages.props with centralized EF Core 9.0.5 versions - TeamCity pipeline YAMLs for general-checks and publish Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
47 lines
1.2 KiB
C#
47 lines
1.2 KiB
C#
using System.Linq.Expressions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace HrynCo.DAL.EF.Core;
|
|
|
|
public abstract class EfRepository<TDbContext, TEntity>
|
|
where TDbContext : DbContext
|
|
where TEntity : class
|
|
{
|
|
protected TDbContext DbContext { get; }
|
|
protected DbSet<TEntity> DbSet { get; }
|
|
|
|
protected EfRepository(TDbContext dbContext)
|
|
{
|
|
DbContext = dbContext;
|
|
DbSet = dbContext.Set<TEntity>();
|
|
}
|
|
|
|
protected async Task AddAsync(TEntity entity, CancellationToken ct = default)
|
|
{
|
|
await DbSet.AddAsync(entity, ct);
|
|
}
|
|
|
|
protected async Task AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken ct = default)
|
|
{
|
|
await DbSet.AddRangeAsync(entities, ct);
|
|
}
|
|
|
|
protected void Update(TEntity entity)
|
|
{
|
|
DbSet.Update(entity);
|
|
}
|
|
|
|
protected void Delete(TEntity entity)
|
|
{
|
|
DbSet.Remove(entity);
|
|
}
|
|
|
|
protected void DeleteRange(IEnumerable<TEntity> entities)
|
|
{
|
|
DbSet.RemoveRange(entities);
|
|
}
|
|
|
|
protected Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken ct = default) =>
|
|
DbSet.AnyAsync(predicate, ct);
|
|
}
|