SaveChangesAsync not saving a specific value when it's assigned

Viewed 212

I've ran into an issue I can't quite seem to figure out. Here I have a ticket item which is assigned to user. I have implemented a feature to be able to change the user. The user is changed and updated correctly however, the UserID value for the ticket remains unchanged despite being reassigned and SaveChangesAsync being called to confirm these changes. .AssignedUser is changed in the database and saved however, the UserID refuses to update in the db. None of the values are null and when setting a breakpoint and running through the function it all seems to assign itself correctly with the correct values. The value UserID just does not save or update. Would appreciate any insight into this. Thanks in advance

Ticket model

public class Ticket
{
    [Key]
    public Guid Id { get; set; }
    
    [Required]
    public string Title { get; set; }

    public Guid UserID { get; set; } //who the ticket is assigned to

    public Guid CreatedByUser { get; set; } //who this ticket is created by

    public ApplicationUser CreatedBy { get; set; } //created by user
    
    public ApplicationUser AssignedUser { get; set; } // who the ticket is assigned to

}

Razor page

[Parameter] public Guid Id { get; set; }
private Ticket ticketItem { get; set; } = new Ticket();
private ApplicationUser AssignUser { get; set; } = new ApplicationUser();

private async Task ChangeUser()
{

try
{
    var response = await Http.PutAsJsonAsync($"Tickets/changeuser/{AssignUser.UserName}/{ticketItem.Id}", AssignUser);

} catch(Exception) { ... }

}

Controller making the changes

    [HttpPut("changeuser/{username}/{ticketID}")]
    public async Task<IActionResult> ChangeTicketUser(string username, Guid ticketID)
    {
        ApplicationUser findUser;
        Ticket ticketItem;
        try {
            findUser = await _userManager.Users.FirstAsync(u => u.Email.Equals(username));
            
        } catch (Exception)
        {
            return Forbid(); //user not found
        }

        try
        {
            ticketItem = await _context.Tickets.Where(t => t.Id.ToString().Equals(ticketID.ToString())).FirstAsync();
        } catch (Exception)
        {
            return BadRequest(); //ticket not found
        }


        ticketItem.AssignedUser = findUser;
        ticketItem.UserID = Guid.Parse(findUser.Id);

        await _context.SaveChangesAsync();

        return NoContent();

    }

Values are different and valid

enter image description here

Requested ApplicationUser Model

public class ApplicationUser : IdentityUser
{
    public string firstName { get; set; }

    public string lastName { get; set; }

    public DateTime LastAccessed { get; set; }
}
0 Answers
Related