植入asp.net核心身份角色

Capiono

我正在尝试为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.EnsureRolesAreCreated();

当上面的代码运行时,CreateAsync函数将引发异常:

{Microsoft.EntityFrameworkCore.DbUpdateException:更新条目时发生错误。有关详细信息,请参见内部异常。---> System.Data.SqlClient.SqlException:无法将值NULL插入表'AspNetRoles'的列'Id'中;列不允许为空。INSERT失败。该语句已终止。

我已经尝试过This and this,仍然无法正常工作。

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。

Capiono

我发现了问题。我不见了:

base.OnModelCreating(modelBuilder);

在ModelCreating函数的开头

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章