在我的一项应用程序服务中,我试图在删除(使用软删除)之前更新记录。 玩了之后,我发现我可以更新记录或删除记录,但是我不能两者都做。最后的操作(在这种情况下为删除)始终优先。
如果我先删除然后再更新,则该记录不会标记为已删除,并且该列也会更新,那么在删除之前如何更新记录url
和displayname
?我添加了UnitOfWork
属性,但似乎没有任何影响
我的方法可以在下面找到。
[AbpAuthorize(AppPermissions.Pages_PmnyDocuments_Delete)]
[UnitOfWork]
public async Task Delete(EntityDto input)
{
var entity = await _pmnyDocumentsRepository.GetAsync(input.Id);
var output = await _fileServerManager.DeleteAsync(new DeleteFileServerObjectInput(PmnyConsts.Bucket, $"{entity.MasterId}/{entity.ParentIdentifier}".AsFilePath(), entity.DisplayName));
entity.Url = output.FilePath;
entity.DisplayName = output.FileName;
await _pmnyDocumentsRepository.UpdateAsync(entity);
var entity2 = await _pmnyDocumentsRepository.GetAsync(entity.Id);
await _pmnyDocumentsRepository.DeleteAsync(entity2.Id);
}
答案 0 :(得分:0)
我会尝试删除[UnitOfWork]
属性,而不是将更新和删除包装在自己的uow中。
using (var unitOfWork = _unitOfWorkManager.Begin())
{
//UPDATE
}
using (var unitOfWork = _unitOfWorkManager.Begin())
{
//DELETE
}
文档here
中有更多信息答案 1 :(得分:-1)
you need to issue a save changes between the two calls , because all commits to the database automaticly happens at the end of your method.
see this quote
SaveChanges ASP.NET Boilerplate saves all changes at the end of a unit of work. You don't have to do anything, but sometimes you may want to save changes to the database in the middle of a unit of work operation. For example, after saving some changes, we may want to get the Id of a newly inserted Entity using EntityFramework.
You can use the SaveChanges or SaveChangesAsync method of the current unit of work.
Note that if the current unit of work is transactional, all changes in the transaction are rolled back if an exception occurs. Even the saved changes!