问题描述
我是Entity Framework的新手,目前正在从事一个项目。我需要将数据库中的表中的数据(坐在多个数组中)更新为新表。
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public virtual CandidatePersonalInfo CandidatePersonalInfo { get; set; }
public ICollection<CandidateEducation> CandidateEducations { get; set; }
}
然后我为2个不同的表提供2个模型:
个人信息(1-1关系):
public class CandidatePersonalInfo
{
public int Id { get; set; }
public int UserId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string Surname { get; set; }
public string Gender { get; set; }
public string Title { get; set; }
public virtual User User { get; set; }
}
应聘者教育模式(一对多关系):
public class CandidateEducation
{
public int Id { get; set; }
public int UserId { get; set; }
public DateTime DateStarted { get; set; }
public DateTime DateCompleted { get; set; }
public string NameOfInstitution { get; set; }
public string CourseName { get; set; }
public User User { get; set; }
}
public class CandidateUpdateDto
{
public virtual CandidatePersonalInfo CandidatePersonalInfo { get; set; }
public ICollection<CandidateEducation> CandidateEducations { get; set; }
}
然后我使用AutoMapper在用户模型和CandidateUpdateDTO之间创建映射:
CreateMap<CandidateUpdateDto,User>();
[HttpPut("{id}")]
public async Task<IActionResult> UpdateUser(int id,CandidateUpdateDto candidateUpdateDto)
{
if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
return Unauthorized();
var userfromrepo = await _repo.GetUser(id);
_mapper.Map(candidateUpdateDto,userfromrepo);
if (await _repo.SaveAll())
return NoContent();
throw new Exception($"Updating user {id} Failed on save");
}
问题:当我在Postman中测试更新时,PUT请求成功,但是它清除了表中的所有数据。有人可以帮助我了解为什么会发生这种情况以及如何解决吗?
感谢一百万
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)