I want to update complex object property in my asp.net core 2 project.
My UserProjects class looks like:
public class UserProject
{
public int UserId { get; set; }
public User User { get; set; }
public int ProjectId { get; set; }
public Project Project { get; set; }
}
My method which I want to change project status from ready to pending. With simple objects I know but with complex objects I can not achieve parameter like entry.ProjectStatus even I will use my _context.Projects. My method looks like:
[HttpPost("changeprojectstatus/{userId}/{projectId}")]
public IActionResult ChangeProjectStatus(int userId, int projectId)
{
var result = _context.UserProjects.Where(x => x.UserId == userId && x.ProjectId ==
projectId);
if (result != null)
{
// Make changes on entity
var pr = result.Where(x => x.Project.Status == ProjectStatus.Ready);
// Update entity in DbSet
_context.Projects.Update(pr);
_context.SaveChanges();
}
if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
return Unauthorized();
return Ok();
}