gpt4 book ai didi

c# - 播种 asp.net 核心身份角色

转载 作者:行者123 更新时间:2023-11-30 14:49:06 29 4
gpt4 key购买 nike

我正在尝试为 AspNetRole 表设置初始系统角色。

播种扩展:

public static void EnsureRolesAreCreated(this IApplicationBuilder app) {
Dictionary<string, string> roles = new Dictionary<string, string>
{
{ "Administrator", "Global Access." },
{ "User", "Restricted to business domain activity." }
};

var context = app.ApplicationServices.GetService<ApplicationDbContext>();
if (context.AllMigrationsApplied()) {
var roleManager = app.ApplicationServices.GetService<ApplicationRoleManager>();
foreach (var role in roles) {
if (!roleManager.RoleExistsAsync(role.Key).Result) {
roleManager.CreateAsync(new ApplicationRole() { Name = role.Key, System = true, Description = role.Value });
}
}
}
}

在启动类中调用:app app.EnsureRolesAreCreated();

当上面的代码运行时,CreateAsync 函数抛出异常:

{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 'AspNetRoles'; column does not allow nulls. INSERT fails. The statement has been terminated.

我试过了Thisthis , 还是行不通。

services.AddIdentity<ApplicationUser, ApplicationRole>(config => {
config.User.RequireUniqueEmail = true;
config.Lockout = new LockoutOptions {
AllowedForNewUsers = true,
DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30),
MaxFailedAccessAttempts = 5
};
config.Password = new PasswordOptions {
RequireDigit = true,
RequireNonAlphanumeric = false,
RequireUppercase = true,
RequireLowercase = true,
RequiredLength = 12,
};
})
.AddEntityFrameworkStores<ApplicationDbContext, int>()
.AddUserValidator<ApplicationUserValidator<ApplicationUser>>()
.AddUserManager<ApplicationUserManager>()
.AddRoleManager<ApplicationRoleManager>()
.AddDefaultTokenProviders();



public class ApplicationUser : IdentityUser<int> {}
public class ApplicationRole : IdentityRole<int> {}


protected override void OnModelCreating(ModelBuilder modelBuilder) {

modelBuilder.Entity<ApplicationUser>(i => {
i.HasKey(x => x.Id);
i.Property(x => x.Id).ValueGeneratedOnAdd();
});
modelBuilder.Entity<ApplicationRole>(i => {
i.HasKey(x => x.Id);
i.Property(x => x.Id).ValueGeneratedOnAdd();
});
modelBuilder.Entity<IdentityUserRole<int>>(i => {
i.HasKey(x => new { x.RoleId, x.UserId });
});

modelBuilder.Entity<IdentityUserLogin<int>>(i => {
i.HasKey(x => new { x.ProviderKey, x.LoginProvider });
});

modelBuilder.Entity<IdentityRoleClaim<int>>(i => {
i.HasKey(x => x.Id);
i.Property(x => x.Id).ValueGeneratedOnAdd();
});

modelBuilder.Entity<IdentityUserClaim<int>>(i => {
i.HasKey(x => x.Id);
i.Property(x => x.Id).ValueGeneratedOnAdd();
});
}

我添加了 ValueGeneratedOnAdd() 以尝试强制生成 ID。

最佳答案

我发现了问题。我失踪了:

base.OnModelCreating(modelBuilder);

在ModelCreating函数的开头

 protected override void OnModelCreating(ModelBuilder modelBuilder){
base.OnModelCreating(modelBuilder);
}

进行更改后,我进行了迁移,ID 列现在创建为标识列

[Id] [int] IDENTITY(1,1) NOT NULL

关于c# - 播种 asp.net 核心身份角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39426560/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com