'Error trying modify some attributes of a database table in a controller C#

I´m trying to modify attributes using sql database and web api .Net 6

In a controller i have this method

private void SetRefreshToken(RefreshToken newRefreshToken)
{

            newRefreshToken = GenerateRefreshToken();

            //...

            user.RefreshToken = newRefreshToken.Token;
            user.TokenCreated = newRefreshToken.Created;
            user.TokenExpires = newRefreshToken.Expires;

            _context.Update(user.RefreshToken);
            _context.Update(user.TokenCreated);
            _context.Update(user.TokenExpires);

            _context.SaveChangesAsync();

            //System.InvalidOperationException: The entity type 'string' was not found.

            //This error occurs if is _context.Update(user)
            //System.ArgumentNullException: Value cannot be null.
}

And I have two models, an User who have some attributes + refreshtoken + Token Created + Token Expires And a Refresh Token Model who have the refreshtoken + Token Created + Token Expires

How I can update some data of a table and get rid of this errors?



Solution 1:[1]

Try retrieving user from database and then populating with new values and save the changes to context.

   var user =_context.users.FirstOrDefault(b => b.id.Equals(user_id));
 
    if (user is not null)
    {
    user.RefreshToken = newRefreshToken.Token;
    user.TokenCreated = newRefreshToken.Created;
    user.TokenExpires = newRefreshToken.Expires;
    _context.SaveChangesAsync();
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Denis Michael Kamukama