提问人:Musa AKYUZ 提问时间:11/11/2023 更新时间:11/11/2023 访问量:53
DbContext Update 不起作用,但 Get 正常工作。为什么?
DbContext Update is not working but Get is working. Why?
问:
我在核心层中实现了 Repository 和 UnitOfWork 模式:
public class Repository<TEntity, TContext> : UnitOfWork, IRepository<TEntity>
where TEntity : class, new()
where TContext : DbContext
{
private readonly TContext _context;
public Repository(TContext dbContext) : base(dbContext)
{
_context = dbContext;
}
public async Task<bool> AddAsync(TEntity model)
{
await _context.Set<TEntity>().AddAsync(model);
return true;
}
public List<TEntity> Get()
{
return _context.Set<TEntity>().ToList();
}
public bool Remove(TEntity model)
{
_context.Set<TEntity>().Remove(model);
return true;
}
public async Task<int> SaveAsync()
{
return await _context.SaveChangesAsync();
}
public bool Update(TEntity model)
{
_context.Set<TEntity>().Update(model);
return true;
}
}
和
public class UnitOfWork : IUnitOfWork
{
private readonly DbContext _context;
private readonly IDbContextTransaction _transaction;
public UnitOfWork(DbContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_transaction = _context.Database.CurrentTransaction ?? _context.Database.BeginTransaction();
}
public async Task<bool> Commit(bool state = true)
{
try
{
await _context.SaveChangesAsync();
if (state)
_transaction.Commit();
else
_transaction.Rollback();
return true;
}
catch (Exception)
{
_transaction.Rollback();
throw; // Rethrow the exception after rolling back the transaction
}
finally
{
Dispose();
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual async void Dispose(bool disposing)
{
if (disposing)
{
await _context.DisposeAsync();
_transaction.Dispose();
}
}
}
因此,我在我的 DataAccess 和 BusinessLayer 中使用了这个类,最后在我的 MVC 控制器中使用了这个类,就像分层结构一样。我可以在 ı 调用 Get() 方法时获取值。但是 ı 试图通过 Update() 方法更改一个值不起作用。尽管 ı 调用 SaveAsync() 方法。
我正在下面共享我的 DataAccess 和业务层。
数据访问:
public class ResponsibleDal : Repository<Responsible, TaskTracingDbContext>, IResponsibleDal
{
public ResponsibleDal(TaskTracingDbContext dbContext) : base(dbContext)
{
}
}
DataAccess 工厂:
public class MainDal : IMainDal
{
private readonly Lazy<IResponsibleDal> _responsibleDalLazy;
public MainDal(IResponsibleDal responsibleDalLazy)
{
_responsibleDalLazy = new Lazy<IResponsibleDal>(() => responsibleDalLazy);
}
public IResponsibleDal ResponsibleDal => _responsibleDalLazy.Value;
}
商:
public class ResponsibleBus : IResponsibleBus
{
private readonly IMainDal _mainDal;
public ResponsibleBus(IMainDal mainDal)
{
_mainDal = mainDal;
}
public async Task<bool> AddAsync(Responsible model)
{
return await _mainDal.ResponsibleDal.AddAsync(model);
}
public List<Responsible> Get()
{
return _mainDal.ResponsibleDal.Get();
}
public bool Remove(Responsible model)
{
return _mainDal.ResponsibleDal.Remove(model);
}
public Task<int> SaveAsync()
{
return _mainDal.ResponsibleDal.SaveAsync();
}
public Responsible? GetById(int id)
{
return _mainDal.ResponsibleDal.Get().Find(x => x.Id == id);
}
public Responsible? GetByIdentityNumber(string? identityNumber)
{
if (identityNumber is null)
return null;
return _mainDal.ResponsibleDal.Get().Find(x => x.IdentityNumber == identityNumber);
}
public bool Update(Responsible model)
{
return _mainDal.ResponsibleDal.Update(model);
}
}
商业工厂:
public class MainBus : IMainBus
{
private readonly Lazy<IResponsibleBus> _responsibleBusLazy;
public MainBus(IResponsibleBus responsibleBusLazy)
{
_responsibleBusLazy = new Lazy<IResponsibleBus>(() => responsibleBusLazy);
}
public IResponsibleBus ResponsibleBus => _responsibleBusLazy.Value;
}
最后在我的控制器中:
var entity = _mainBus.ResponsibleBus.GetByIdentityNumber(model.UserName);
if (entity is not null)
{
entity.ThemeSettings = model.Theme;
_mainBus.ResponsibleBus.Update(entity);
await _mainBus.ResponsibleBus.SaveAsync();
HttpContext.Session.SetString("Theme", model.Theme.ToString());
}
else
{
TempData["Error"] = "Kullanıcı veritabanında bulunamadı";
return RedirectToAction(nameof(Index), model);
}
我也在 Program.cs 中这样确定范围:
builder.Services.AddDbContext<TaskTracingDbContext>();
builder.Services.AddTransient<IResponsibleDal, ResponsibleDal>();
builder.Services.AddTransient(provider => new Lazy<IResponsibleDal>(provider.GetRequiredService<IResponsibleDal>()));
builder.Services.AddTransient<IResponsibleBus, ResponsibleBus>();
builder.Services.AddTransient(provider => new Lazy<IResponsibleBus>(provider.GetRequiredService<IResponsibleBus>()));
builder.Services.AddTransient<IMainDal, MainDal>();
builder.Services.AddTransient<IMainBus, MainBus>();
你能向我解释一下我不知道的细节吗?
答:
0赞
Musa AKYUZ
11/11/2023
#1
我解决了。因为 Repository 类中的方法。我不得不在类下调用方法。 方法保存更改。SaveAsync()
Commit()
UnitOfWork
Commit()
public async Task<bool> SaveAsync()
{
return await Commit();
}
评论
0赞
Musa AKYUZ
11/11/2023
如果有人解释得更详细。我谢谢你。
1赞
José Ramírez
11/12/2023
DbContext 已经是 Repository 和 UnitOfWork 模式的实现。没有必要重复,除非你想准备你的代码,以便将来将 EF 交换为其他东西。
评论