I am using ASP.NET Core 6, and when I try to add Claims into table UserClaims I get this error:
Cannot insert the value NULL into column UserId
table UserClaims column does not allow nulls
UPDATE fails
Code:
var user = await _userManager.FindByIdAsync(id);
var result = await _userManager.AddClaimsAsync(user, allSelectedClaims);
The error message is indicating that the UserId column in the UserClaims table does not allow null values, but you’re trying to insert a null value.
This is likely because the user object you’re retrieving from _userManager.FindByIdAsync(id)
is null.
Try this one –
var user = await _userManager.FindByIdAsync(id);
if (user == null)
{
throw new NotFoundException("User not found");
}
var result = await _userManager.AddClaimsAsync(user, allSelectedClaims);