SqlException:无效的列名'NormalizedName'。无效的列名“ ConcurrencyStamp”。无效的列名“ NormalizedName”

时间:2020-08-17 18:20:32

标签: asp.net asp.net-core asp.net-identity

为什么在.NET Core Identity上使用PasswordSignInAsync时会出现异常? Microsoft.AspNetCore.Identity

await _signInManager.PasswordSignInAsync(user.UserName, Input.Password, Input.RememberMe, lockoutOnFailure: false);

An unhandled exception occurred while processing the request.
SqlException: Invalid column name 'NormalizedName'.
Invalid column name 'ConcurrencyStamp'.
Invalid column name 'NormalizedName'.
Microsoft.Data.SqlClient.SqlCommand+<>c.<ExecuteDbDataReaderAsync>b__164_0(Task<SqlDataReader> result)

我可以创建用户,重设密码等。 'NormalizedName'甚至都不是Microsoft.AspNetCore.Identity的一部分。所有列都存在于我的表中

select LockoutEnd, TwoFactorEnabled, PhoneNumberConfirmed, PhoneNumber, ConcurrencyStamp, SecurityStamp, 
PasswordHash, EmailConfirmed, NormalizedEmail, Email, NormalizedUserName, UserName, Id, LockoutEnabled, AccessFailedCount
from [dbo].[AspNetUsers]

1 个答案:

答案 0 :(得分:0)

该异常实际上来自AspNetRoles表,而不是AspNetUsers表。如果您已将旧的Asp.Net Identity迁移到Asp.Net Core,则需要在“角色”表中添加两个新字段。这是一个迁移:

public partial class Identity : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AddColumn<string>(
            name: "NormalizedName",
            table: "AspNetRoles",
            type: "nvarchar(256)",
            maxLength: 256,
            nullable: true);

        migrationBuilder.AddColumn<string>(
            name: "ConcurrencyStamp",
            table: "AspNetRoles",
            type: "nvarchar(max)",
            nullable: true);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "ConcurrencyStamp",
            table: "AspNetRoles");

        migrationBuilder.DropColumn(
            name: "NormalizedName",
            table: "AspNetRoles");
    }
}

如果您想进行完整的Identity迁移,包括用户,声明和其他表格,可以参考我对这个问题的回答:https://stackoverflow.com/a/65003440/1348324

相关问题