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?
CodePudding user response:
Problem:
_context.Update(user.RefreshToken);
_context.Update(user.TokenCreated);
_context.Update(user.TokenExpires);
Solution:
_context.Update(user);
CodePudding user response:
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();
}