Copy from #9433
AddClaimAsync failed with SqlException: Cannot insert the value NULL into column 'Id', table 'dbo.AspNetUserClaims'; column does not allow nulls
dotnet 2.1.403
NETCore.App 2.1
AspNetCore.App 2.1.5
According to the instructions in the official document。
Persist additional claims and tokens from external providers in ASP.NET Core
when i use code like
await _userManager.AddClaimAsync(user, Principal.FindFirst(ClaimTypes.Gender));
i get the error like
SqlException: Cannot insert the value NULL into column 'Id', table 'guandian.dbo.AspNetUserClaims'; column does not allow nulls. INSERT fails.
DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@guardrex please review. cc @niltor
I run into same error and was able to identify reason.
The issue happened, because I created initial ModelSnapshot against SQLite database. Later switching to SQL Server, it was crashing with the same error and I deleted ModelSnapshot and re-created again - against current SQL Server.
The difference in shapshot file was in lines related to keys:
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
for tables IdentityRoleClaim and IdentityUserClaim.
So running update-database with fresh ModelSnapshot solved the issue for me.
Closing for now unless we find this is a frequent error.
Most helpful comment
I run into same error and was able to identify reason.
The issue happened, because I created initial ModelSnapshot against SQLite database. Later switching to SQL Server, it was crashing with the same error and I deleted ModelSnapshot and re-created again - against current SQL Server.
The difference in shapshot file was in lines related to keys:
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
for tables IdentityRoleClaim and IdentityUserClaim.
So running update-database with fresh ModelSnapshot solved the issue for me.