zoukankan      html  css  js  c++  java
  • NetCore HttpPatch EFCore 部分修改

    参考博文 :https://www.cnblogs.com/lwqlun/p/10433615.html
    首先 安装Nuget包
    Microsoft.AspNetCore.JsonPatch

    有一个注意事项 必须使用 NewtonsoftJson 使用System.Text.Json 会出现422的模型错误


    Service 层代码
    
            public async Task<IndustryTypeDto> PartUpdateAsync(int id, JsonPatchDocument<IndustryTypeForUpdate> target, UserInfo user, CancellationToken cancellationToken)
            {
    
                var tran = await BeginTransactionAsync(cancellationToken);
                try
                {
                    var existingEntity = await Repository<IIndustryTypeRepository>().LoadEntityAsync(id, cancellationToken);
    
    
                    var modifyEntity = ExpressMapper.Mapper.Map<IndustryType, IndustryTypeForUpdate>(existingEntity);
              target.ApplyTo(modifyEntity);
    ExpressMapper.Mapper.Map(modifyEntity, existingEntity);
    await Repository<IIndustryTypeRepository>().UpdateAsync(existingEntity, cancellationToken); if (tran != null) { await tran.CommitAsync(cancellationToken); } return await Repository<IIndustryTypeRepository>().GetAsync(id, true, cancellationToken); } catch { if (tran != null) { await tran.RollbackAsync(cancellationToken); } throw; } }

    Controller 代码

    [HttpPatch("{id}")]
            public async Task<IActionResult> PartModify(int id, [FromBody] JsonPatchDocument<IndustryTypeForUpdate> modifyModel,CancellationToken cancellationToken)
            {
               var callResult=await  _service.PartUpdateAsync(id,modifyModel,Identity,cancellationToken);
                return Ok(callResult);
            }

    效果图:

    原有值

    patch请求后效果图:

  • 相关阅读:
    错误: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. 的处理方法
    C语言习题
    嵌入式芯片STM32F407
    c语言课后习题
    求方程式的根
    C语言课后习题
    LINUX常用指令
    在 pythonanywhere 上搭建 django 程序(Virtualenv+python2.7+django1.8)
    Git远程操作详解
    ./configure,make,make install的作用
  • 原文地址:https://www.cnblogs.com/litianfeng-net/p/14759517.html
Copyright © 2011-2022 走看看