Aspnetcore.docs: AddClaimAsync failed with SqlException: Cannot insert the value NULL into column 'Id', table 'dbo.AspNetUserClaims'; column does not allow nulls

Created on 8 Nov 2018  Â·  3Comments  Â·  Source: dotnet/AspNetCore.Docs

Copy from #9433

Issue description

AddClaimAsync failed with SqlException: Cannot insert the value NULL into column 'Id', table 'dbo.AspNetUserClaims'; column does not allow nulls

Software versions

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.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P3 Source - Docs.ms

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.

All 3 comments

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

royshouvik picture royshouvik  Â·  3Comments

danroth27 picture danroth27  Â·  3Comments

StevenTCramer picture StevenTCramer  Â·  3Comments

Rick-Anderson picture Rick-Anderson  Â·  3Comments

nenmyx picture nenmyx  Â·  3Comments