向 ASP.Net Identity Core 用户添加声明失败,出现 EF 插入错误
Adding Claims to an ASP.Net Identity Core user fails with EF insert error
我在 .Net Core 上使用 ASP.Net Core,版本:
> dotnet --info
.NET Command Line Tools (2.1.4)
Product Information:
Version: 2.1.4
Commit SHA-1 hash: 5e8add2190
Runtime Environment:
OS Name: Windows
OS Version: 10.0.16299
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk.1.4\
Microsoft .NET Core Shared Framework Host
Version : 2.0.5
Build : 17373eb129b3b05aa18ece963f8795d65ef8ea54
我正在尝试使用声明添加新用户:
var r = await _userManager.CreateAsync(new ApplicationUser { UserName = "example", Email = "example@example.com" }, "password");
var u = await userManager.FindByEmailAsync("example@example.com");
if(u == null) {
throw new Exception("User was not found");
}
await _userManager.AddClaimAsync(u, new Claim("FullName", "Example McExample"));
我正在使用 MS SQL 作为后备存储,Entity Framework 使用命令创建的架构:
dotnet ef database update
用户已在数据库中创建,但声明创建失败并出现错误:
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred
while updating the entries. See the inner exception for details. --->
System.Data.SqlClient.SqlException: Cannot insert the value NULL into
column 'Id', table 'IdentityDemoDatabase.dbo.AspNetUserClaims'; column does
not allow nulls. INSERT fails.
The statement has been terminated.
单步执行代码时,成功创建用户,然后成功检索用户(返回的用户与刚刚添加的用户匹配),然后 AddClaimAsync()
方法失败并出现上述错误。
我做错了什么?
事实证明,我使用了错误的 EF 迁移。
EF 使用您在 Startup
class 中配置的数据库上下文,并且它生成的迁移非常特定于那里配置的提供程序。
我使用的是从 dotnet new --auth individual
模板复制的迁移,但这些是为 Sqlite 构建的,而不是我使用的 MS SQL 服务器。
要生成您自己的迁移,请删除现有的迁移,然后配置您的 Startup
以使用您想要的提供程序:
// Add a store context for ASP.Net Identity 2 to use
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));
然后将 EF 工具(如果它不在您的项目中)添加到您的 csproj
:
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
然后运行:
dotnet restore
dotnet ef migrations add InitialCreate
dotnet ef database update
EF 将 运行 您的 Startup
,获取提供程序和上下文,然后在您的数据库中创建表。
Delete the existing migrations
使用
dotnet ef database update 0
和
dotnet ef migrations remove
我在 .Net Core 上使用 ASP.Net Core,版本:
> dotnet --info
.NET Command Line Tools (2.1.4)
Product Information:
Version: 2.1.4
Commit SHA-1 hash: 5e8add2190
Runtime Environment:
OS Name: Windows
OS Version: 10.0.16299
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk.1.4\
Microsoft .NET Core Shared Framework Host
Version : 2.0.5
Build : 17373eb129b3b05aa18ece963f8795d65ef8ea54
我正在尝试使用声明添加新用户:
var r = await _userManager.CreateAsync(new ApplicationUser { UserName = "example", Email = "example@example.com" }, "password");
var u = await userManager.FindByEmailAsync("example@example.com");
if(u == null) {
throw new Exception("User was not found");
}
await _userManager.AddClaimAsync(u, new Claim("FullName", "Example McExample"));
我正在使用 MS SQL 作为后备存储,Entity Framework 使用命令创建的架构:
dotnet ef database update
用户已在数据库中创建,但声明创建失败并出现错误:
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. --->
System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Id', table 'IdentityDemoDatabase.dbo.AspNetUserClaims'; column does not allow nulls. INSERT fails.
The statement has been terminated.
单步执行代码时,成功创建用户,然后成功检索用户(返回的用户与刚刚添加的用户匹配),然后 AddClaimAsync()
方法失败并出现上述错误。
我做错了什么?
事实证明,我使用了错误的 EF 迁移。
EF 使用您在 Startup
class 中配置的数据库上下文,并且它生成的迁移非常特定于那里配置的提供程序。
我使用的是从 dotnet new --auth individual
模板复制的迁移,但这些是为 Sqlite 构建的,而不是我使用的 MS SQL 服务器。
要生成您自己的迁移,请删除现有的迁移,然后配置您的 Startup
以使用您想要的提供程序:
// Add a store context for ASP.Net Identity 2 to use
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));
然后将 EF 工具(如果它不在您的项目中)添加到您的 csproj
:
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
然后运行:
dotnet restore
dotnet ef migrations add InitialCreate
dotnet ef database update
EF 将 运行 您的 Startup
,获取提供程序和上下文,然后在您的数据库中创建表。
Delete the existing migrations
使用
dotnet ef database update 0
和
dotnet ef migrations remove