From 4aaf4d5b140bc332b067822295f44db10a1eba4a Mon Sep 17 00:00:00 2001 From: Konstantin Gamayunov Date: Fri, 5 Apr 2024 17:09:22 +0300 Subject: [PATCH] db context made protected --- .../KirelGenericEntityFrameworkRepository.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Kirel.Repositories.EntityFramework/KirelGenericEntityFrameworkRepository.cs b/src/Kirel.Repositories.EntityFramework/KirelGenericEntityFrameworkRepository.cs index e894a4d..d223771 100644 --- a/src/Kirel.Repositories.EntityFramework/KirelGenericEntityFrameworkRepository.cs +++ b/src/Kirel.Repositories.EntityFramework/KirelGenericEntityFrameworkRepository.cs @@ -17,7 +17,10 @@ public class KirelGenericEntityFrameworkRepository : where TKey : IComparable, IComparable, IEquatable where TDbContext : DbContext { - private readonly TDbContext _dbContext; + /// + /// Db context + /// + protected readonly TDbContext DbContext; /// /// Reader field for read access /// @@ -30,7 +33,7 @@ protected virtual IQueryable Reader /// protected virtual DbSet Writer { - get => _dbContext.Set(); + get => DbContext.Set(); } /// /// GenericRepository constructor @@ -38,7 +41,7 @@ protected virtual DbSet Writer /// DbContext public KirelGenericEntityFrameworkRepository(TDbContext context) { - _dbContext = context; + DbContext = context; } /// /// Add new Entity @@ -48,8 +51,8 @@ public KirelGenericEntityFrameworkRepository(TDbContext context) public async Task Insert(TEntity entity) { Writer.Add(entity); - _dbContext.Entry(entity).State = EntityState.Added; - await _dbContext.SaveChangesAsync(); + DbContext.Entry(entity).State = EntityState.Added; + await DbContext.SaveChangesAsync(); return entity; } /// @@ -74,7 +77,7 @@ public async Task Delete(TKey id) if (entity == null) throw new KeyNotFoundException($"Entity with specified id {id} was not found"); Writer.Remove(entity); - await _dbContext.SaveChangesAsync(); + await DbContext.SaveChangesAsync(); } /// /// Updating entity @@ -84,8 +87,8 @@ public async Task Delete(TKey id) public async Task Update(TEntity entity) { Writer.Attach(entity); - _dbContext.Entry(entity).State = EntityState.Modified; - await _dbContext.SaveChangesAsync(); + DbContext.Entry(entity).State = EntityState.Modified; + await DbContext.SaveChangesAsync(); return entity; } ///