@@ -62,12 +62,16 @@ public interface IRepository<TEntity>
6262 where TEntity : class
6363{
6464 IQueryable <TEntity > GetAll ();
65+ IQueryable <TEntity > GetAllWithTacking ();
6566 IQueryable <TEntity > GetWhere (Expression <Func <TEntity , bool >> expression );
67+ IQueryable <TEntity > GetWhereWithTracking (Expression <Func <TEntity , bool >> expression );
6668 Task <TEntity > GetByExpressionAsync (Expression <Func <TEntity , bool >> expression , CancellationToken cancellationToken = default );
69+ Task <TEntity > GetByExpressionWithTrackingAsync (Expression <Func <TEntity , bool >> expression , CancellationToken cancellationToken = default );
6770 Task <TEntity > GetFirstAsync (CancellationToken cancellationToken = default );
6871 Task <bool > AnyAsync (Expression <Func <TEntity , bool >> expression , CancellationToken cancellationToken = default );
6972 bool Any (Expression <Func <TEntity , bool >> expression );
7073 TEntity GetByExpression (Expression <Func <TEntity , bool >> expression );
74+ TEntity GetByExpressionWithTracking (Expression <Func <TEntity , bool >> expression );
7175 TEntity GetFirst ();
7276 Task AddAsync (TEntity entity , CancellationToken cancellationToken = default );
7377 void Add (TEntity entity );
@@ -80,7 +84,9 @@ public interface IRepository<TEntity>
8084 void DeleteRange (ICollection <TEntity > entities );
8185
8286}
87+ ```
8388
89+ ``` Csharp
8490public class Repository <TEntity , TContext > : IRepository <TEntity >
8591 where TEntity : class
8692 where TContext : DbContext
@@ -146,6 +152,11 @@ public class Repository<TEntity, TContext> : IRepository<TEntity>
146152 return Entity .AsNoTracking ().AsQueryable ();
147153 }
148154
155+ public IQueryable <TEntity > GetAllWithTacking ()
156+ {
157+ return Entity .AsQueryable ();
158+ }
159+
149160 public TEntity GetByExpression (Expression <Func <TEntity , bool >> expression )
150161 {
151162 TEntity entity = Entity .Where (expression ).AsNoTracking ().FirstOrDefault ();
@@ -156,7 +167,19 @@ public class Repository<TEntity, TContext> : IRepository<TEntity>
156167 {
157168 TEntity entity = await Entity .Where (expression ).AsNoTracking ().FirstOrDefaultAsync (cancellationToken ).ConfigureAwait (false );
158169 return entity ;
159- }
170+ }
171+
172+ public TEntity GetByExpressionWithTracking (Expression <Func <TEntity , bool >> expression )
173+ {
174+ TEntity entity = Entity .Where (expression ).FirstOrDefault ();
175+ return entity ;
176+ }
177+
178+ public async Task <TEntity > GetByExpressionWithTrackingAsync (Expression <Func <TEntity , bool >> expression , CancellationToken cancellationToken = default )
179+ {
180+ TEntity entity = await Entity .Where (expression ).FirstOrDefaultAsync (cancellationToken ).ConfigureAwait (false );
181+ return entity ;
182+ }
160183
161184 public TEntity GetFirst ()
162185 {
@@ -175,6 +198,11 @@ public class Repository<TEntity, TContext> : IRepository<TEntity>
175198 return Entity .AsNoTracking ().Where (expression ).AsQueryable ();
176199 }
177200
201+ public IQueryable <TEntity > GetWhereWithTracking (Expression <Func <TEntity , bool >> expression )
202+ {
203+ return Entity .Where (expression ).AsQueryable ();
204+ }
205+
178206 public void Update (TEntity entity )
179207 {
180208 Entity .Update (entity );
@@ -193,6 +221,6 @@ public class Repository<TEntity, TContext> : IRepository<TEntity>
193221public interface IUnitOfWork
194222{
195223 Task <int > SaveChangesAsync (CancellationToken cancellationToken = default );
196- void SaveChanges ();
224+ int SaveChanges ();
197225}
198226```
0 commit comments