@@ -4,7 +4,7 @@ This library was created by .Net 8.0
44
55## Install
66``` bash
7- dotnet add package EntityFrameworkCore.GenericRepository.Nuget
7+ dotnet add package TS. EntityFrameworkCore.GenericRepository
88```
99
1010## UnitOfWork Implementation
@@ -40,6 +40,12 @@ public async Task AddAsync(User user, CancellationToken cancellationToken)
4040 await _unitOfWork .SaveChangesAsync (cancellationToken );
4141}
4242
43+ public async Task < User ? > GetByIdAsync (Guid id , CancellationToken cancellationToken )
44+ {
45+ User ? user = await _userRepository .FirstOrDefaultAsync (p => p .Id == id , cancellationToken );
46+ return user ;
47+ }
48+
4349public async Task < IList < User >> GetAllAsync (CancellationToken cancellationToken )
4450{
4551 IList < User > users = await _userRepository .GetAll ().ToListAsync (cancellationToken );
@@ -64,7 +70,8 @@ public interface IRepository<TEntity>
6470 IQueryable <TEntity > GetAll ();
6571 IQueryable <TEntity > GetAllWithTracking ();
6672 IQueryable <TEntity > Where (Expression <Func <TEntity , bool >> expression );
67- IQueryable <TEntity > WhereWithTracking (Expression <Func <TEntity , bool >> expression );
73+ IQueryable <TEntity > WhereWithTracking (Expression <Func <TEntity , bool >> expression );
74+ Task <TEntity > FirstOrDefaultAsync (Expression <Func <TEntity , bool >> expression , CancellationToken cancellationToken = default , bool isTrackingActive = true );
6875 Task <TEntity > GetByExpressionAsync (Expression <Func <TEntity , bool >> expression , CancellationToken cancellationToken = default );
6976 Task <TEntity > GetByExpressionWithTrackingAsync (Expression <Func <TEntity , bool >> expression , CancellationToken cancellationToken = default );
7077 Task <TEntity > GetFirstAsync (CancellationToken cancellationToken = default );
@@ -157,6 +164,21 @@ public class Repository<TEntity, TContext> : IRepository<TEntity>
157164 return Entity .AsQueryable ();
158165 }
159166
167+ public async Task <TEntity > FirstOrDefaultAsync (Expression <Func <TEntity , bool >> expression , CancellationToken cancellationToken = default , bool isTrackingActive = true )
168+ {
169+ TEntity entity ;
170+ if (isTrackingActive )
171+ {
172+ entity = await Entity .Where (expression ).FirstOrDefaultAsync (cancellationToken );
173+ }
174+ else
175+ {
176+ entity = await Entity .Where (expression ).AsNoTracking ().FirstOrDefaultAsync (cancellationToken );
177+ }
178+
179+ return entity ;
180+ }
181+
160182 public TEntity GetByExpression (Expression <Func <TEntity , bool >> expression )
161183 {
162184 TEntity entity = Entity .Where (expression ).AsNoTracking ().FirstOrDefault ();
0 commit comments