【发布时间】:2019-08-03 10:01:13
【问题描述】:
我在我的 ClientUserController 中创建了一个补丁方法,但是 patchDoc 没有更新作为 ClientUser 实体的子用户实体的属性。 Post 方法工作正常,它正在更新我映射的 ClientUser 实体的每个子属性。但是,由于某种原因,这不适用于 PATCH 方法。
用户
public int Id { get; set; }
public Genders Gender { get; set; }
[MaxLength(30)]
public string FirstName { get; set; }
[MaxLength(30)]
public string LastName { get; set; }
public DateTime BirthDay { get; set; }
[EmailAddress]
public string Email { get; set; }
[Phone]
public string Phone { get; set; }
public string Address { get; set; }
[ForeignKey("CityId")]
public City City { get; set; }
public int CityId { get; set; }
客户用户 公共 int ID { 获取;放; }
[ForeignKey("UserId")]
public User User { get; set; }
public int UserId { get; set; }
[EmailAddress]
public string Email { get; set; }
[Phone]
[MaxLength(15)]
public string Phone { get; set; }
[ForeignKey("DepartmentId")]
public Department Department { get; set; }
public int DepartmentId { get; set; }
public string JobTitle { get; set; }
[ForeignKey("ClientId")]
public Client Client { get; set; }
public int ClientId { get; set; }
ClientUserController(简单)
[HttpPatch("{id}", Name = "PatchClientUser")]
public async Task<IActionResult> PatchClientUser(int clientId, int id,
[FromBody] JsonPatchDocument<ClientUserForUpdateDto> patchDoc)
{
if (patchDoc == null)
{
return BadRequest();
}
var clientUserFromRepo = await _clientUserRepository.GetClientUserByIdAsync(clientId, id);
if (clientUserFromRepo == null)
{
return NotFound("Client User Not Found");
}
var clientUserToPatch = Mapper.Map<ClientUserForUpdateDto>(clientUserFromRepo);
patchDoc.ApplyTo(clientUserToPatch);
Mapper.Map(clientUserToPatch, clientUserFromRepo);
_clientUserRepository.UpdateEntity(clientUserFromRepo);
await _clientUserRepository.SaveChangesAsync();
return NoContent();
}
客户用户资料
public class ClientUserProfile : Profile
{
public ClientUserProfile()
{
CreateMap<ClientUser, ClientUserDto>();
CreateMap<ClientUserForCreationDto, ClientUser>();
CreateMap<ClientUser, ClientUserForUpdateDto>(MemberList.Destination);
CreateMap<ClientUserForUpdateDto, ClientUser>(MemberList.Source);
}
用户资料
public class UserProfile : Profile
{
public UserProfile()
{
CreateMap<User, UserDto>();
CreateMap<UserForCreationDto, User>();
CreateMap<User, UserForUpdateDto>(MemberList.Destination);
CreateMap<UserForUpdateDto, User>(MemberList.Source);
}
}
ClientUser 的 POST 方法和 GET 方法工作正常。
我的 ClientUserForUpdateDto 如下所示:
public string UserFirstname { get; set; }
public string UserLastname { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public int DepartmentId { get; set; }
public string JobTitle { get; set; }
在邮递员的 requestBody 中,我有以下内容:
[
{
"op": "replace",
"path": "/userLastname",
"value": "Some lastname"
}
]
如果我想通过其 Id 更改 clientUser 的 LastName 并正确映射它,这将是一种方式。
在 Postman 作为响应中,我得到了正确的 NoContent 响应。但是,ClientUser 没有更新。
【问题讨论】:
标签: c# asp.net-core asp.net-core-mvc