HTTP PATCH - one DTO to patch multiple Entities

wavemaster 311 Reputation points
2021-06-07T18:01:24.37+00:00

My current implementation has two http.PatchAsync() calls to the Web API, i.e. to multiple controller action methods, one for each Entity.

Thought that I could improve on this by sending one DTO containing the data the needs to be updated for multiple Entities, only one packet at risk of loss vs. multiple.

Now I have hit a road block as I cannot find how to use the same JsonPatchDocument for several Entities

Code so far:

[Route("UpdatePartial/{id}")]  /
        [HttpPatch]
        public async Task<IActionResult> Patch(int id, bool hasQbWc, [FromBody] JsonPatchDocument<TxnBillingPatch> txnPatch)
        {
            if (txnPatch == null) { return BadRequest(); }

            var theTxn = context.Transactions.FirstOrDefault(t => t.TransactionId == id);

            if (theTxn == null) { return NotFound(); }
            **txnPatch.ApplyTo(theTxn, ModelState);**

            var isValid = TryValidateModel(theTxn);
            if (!isValid) { return BadRequest(ModelState);  }

            //await context.SaveChangesAsync();
            return NoContent();
        }

Squiggles on the bolded line with "cannot convert from Transaction (the Entity) to TxnBillingPatch (the DTO)

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,560 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rena Ni - MSFT 2,066 Reputation points
    2021-07-01T03:15:36.847+00:00

    Hi @wavemaster ,

    You need change like below:

        [Route("UpdatePartial/{id}")]  
        [HttpPatch]  
        public async Task<IActionResult> Patch(int id, bool hasQbWc, [FromBody] JsonPatchDocument<TxnBillingPatch> txnPatch)  
        {  
            if (txnPatch == null) { return BadRequest(); }  
            var theTxn = new Transaction() { Id = 1, TransName = "trans1" };  
              
           //add this....  
            var data = new TxnBillingPatch();        
            data.Transaction = theTxn;  
            txnPatch.ApplyTo(data, ModelState);   
      
            var isValid = TryValidateModel(theTxn);  
            if (!isValid) { return BadRequest(ModelState); }  
      
            //await context.SaveChangesAsync();  
            return new ObjectResult(data);  
        }  
    

    JSON patch example:

    [  
        {  
            "op":"add",  
            "path":"/TestViewModel",  
            "value":[{  
                "id":1,  
                "testName":"aa"  
            }]  
        }  
    ]  
    

    Result:

    {  
        "transaction": {  
            "id": 1,  
            "transName": "trans1"  
        },  
        "testViewModel": [  
            {  
                "id": 1,  
                "testName": "aa"  
            }  
        ]  
    }  
    

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    Rena


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.