EF Core 新增数据后立即修改该条数据

 

Abp.Core 的方法,是自带事务的,当存在同一个方法新增一条数据后又再次修改此条数据,则会提示异常,不存在此条数据

因为新增还没有提交,数据库中是不存在此条数据的,所以需要,提交一次事务即可

/// <summary>
/// 新增信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<JsonResultModel> CreateInfo(CreateFlowInfoDto input)
{
FlowInfo model = ObjectMapper.Map<FlowInfo>(input);

//新增

model.Id = await this.Repository.InsertAndGetIdAsync(model);

if (string.IsNullOrWhiteSpace(model.Id.ToString()))
{
     return new JsonResultModel() { Message = $"操作失败", Success = false };
 }
//新增流程Json表
FlowDefines flowDefines = new FlowDefines() { FlowInfoId = model.Id, JsonRemark = input.JsonRemark };
flowDefines.Id = await this._flowDefinesRepository.InsertAndGetIdAsync(flowDefines);

//反写字段Id

if (!string.IsNullOrWhiteSpace(model.Id.ToString()))
{
       //由于ABP.core 方法自带事务,需要先提交之前的事务,才可操作修改,否则提示不存在此数据
        CurrentUnitOfWork.SaveChanges();
        await this.Repository.UpdateAsync(model.Id, async entity => { entity.FlowDefinesId = flowDefines.Id; });
}

     return new JsonResultModel() { Message = $"操作成功", Success = true };
}

posted @ 2022-12-06 17:30  liaer  阅读(273)  评论(0)    收藏  举报