如何将 Microsoft.AspNetCore.JsonPatch 与模型和 EF 实体一起使用

问题描述 投票:0回答:1

我的 ASP.NET Core Web API 中有以下方法。它使用

Microsoft.AspNetCore.JsonPatch
进行修补:

[HttpPatch]
public IActionResult PatchCustomer([FromBody] JsonPatchDocument<CustomerModel> patchDoc)
{
    if (patchDoc != null)
    {
        var customerEntity = _dbContext.Customers.Where(x=>x.CustomerID == id);

        // the ApplyTo() method below throws compilation error because of type mismatch
        patchDoc.ApplyTo(customerEntity, ModelState);

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
          
        return Ok();
    }
    else
    {
        return BadRequest(ModelState);
    }
}

我可以通过将方法参数更改为类型

JsonPatchDocument<Customer>
来修复错误,其中
Customer
在实体中。但是,我不想将实体暴露给 API 方法。
CustomerModel
类型仅具有调用者可以更新的某些属性,因此使用
CustomerModel
作为参数可以最大限度地降低调用者意外更新某些其他属性的风险。

这里如何根据

Customer
更新
JsonPatchDocument
实体?

asp.net-core asp.net-core-webapi json-patch
1个回答
0
投票

您可以尝试使用

Automapper
CustomerModel
映射到
Customer
实体,然后应用
JsonPatchDocument
。或者使用Linq查询将
Customer
实体的属性映射到
CustomerModel的属性
模型对象。然后将
JsonPatchDocument
应用到
CustomerModel
,并将修改后的属性值更新到物理对象。您可以参考以下示例:

我的客户:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
}

我的卡斯特模型:

public class CustomerModel
{
     public string Name { get; set; }
     public string Email { get; set; }

     public string Phone {  get; set; }
}

我的方法(useAutomapper):

    [HttpPatch]
    public IActionResult PatchCustomer([FromBody] JsonPatchDocument<CustomerModel> patchDoc, int id)
    {
        if (patchDoc != null)
        {
            var customerEntity = _dbContext.Customers
                .FirstOrDefault(x => x.Id == id);

            if (customerEntity == null)
            {
                return NotFound();
            }

            var customerModel = _mapper.Map<CustomerModel>(customerEntity); 

            patchDoc.ApplyTo(customerModel, ModelState);

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _mapper.Map(customerModel, customerEntity); 

            _dbContext.SaveChanges();

            return Ok();
        }
        else
        {
            return BadRequest(ModelState);
        }
    }

需要映射的实体:

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {
        CreateMap<Customer, CustomerModel>(); 
        CreateMap<CustomerModel, Customer>(); 
    }
}

在program.cs中:

builder.Services.AddAutoMapper(typeof(AutoMapperProfile));

使用Linq查询(需要注意的是,该方法需要属性名称和类型完全匹配):

[HttpPatch]
public IActionResult PatchCustomer([FromBody] JsonPatchDocument<CustomerModel> patchDoc, int id)
{
     if (patchDoc != null)
     {
         var customerEntity = _dbContext.Customers
             .FirstOrDefault(x => x.Id == id);

         if (customerEntity == null)
         {
             return NotFound();
         }

         var customerModel = new CustomerModel
         {
             Name = customerEntity.Name,
             Email = customerEntity.Email,
             Phone =customerEntity.Phone,
         };

         patchDoc.ApplyTo(customerModel, ModelState);

         if (!ModelState.IsValid)
         {
             return BadRequest(ModelState);
         }
         customerEntity.Name = customerModel.Name;
         customerEntity.Email = customerModel.Email;
         customerEntity.Phone = customerModel.Phone;
         _dbContext.SaveChanges();

         return Ok();
     }
     else
     {
         return BadRequest(ModelState);
     }
}
© www.soinside.com 2019 - 2024. All rights reserved.