将更改后的种子方法中的新角色添加到字符串到int [英] Can't add new role on Seed method after change Identity from string to int

查看:85
本文介绍了将更改后的种子方法中的新角色添加到字符串到int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循这个文章在ASP.NET身份中更改用户的主键当在种子方法中添加新角色时,我有一个问题。


无法将值NULL插入列'Id',表'X.dbo.AspNetRoles';列不允许为空。 INSERT失败。

该语句已被终止。


我的代码是:

  protected override void Seed(ApplicationDbContext context)
{
var roleStore = new CustomRoleStore(context);
var roleManager = new ApplicationRoleManager(roleStore);

if(!roleManager.RoleExists(Administrador))
roleManager.Create(new CustomRole(Administrador));
}

错误发生在最后一行:roleManager.Create ...



加上教程我在IdentityConfig.cs中有这个实现:

  public class ApplicationRoleManager:RoleManager< CustomRole,int> 
{
public ApplicationRoleManager(IRoleStore< CustomRole,int> roleStore)
:base(roleStore){}

public static ApplicationRoleManager创建(IdentityFactoryOptions< ApplicationRoleManager> IOwinContext context)
{
return new ApplicationRoleManager(new RoleStore< CustomRole,int,CustomUserRole>(context.Get< ApplicationDbContext>()));
}
}

  public partial class ApplicationDbContext:IdentityDbContext< ApplicationUser,CustomRole,int,CustomUserLogin,CustomUserRole,CustomUserClaim> 
{
public ApplicationDbContext()
:base(DefaultConnection)
{
}

public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}


解决方案

可以为此做一些工作。您可以手动更新数据库上的CustomRole表以具有自动增量值。然后覆盖EF OnModelCreating,如下所述。

  public partial class ApplicationDbContext:IdentityDbContext< ApplicationUser,CustomRole,int,CustomUserLogin,CustomUserRole, CustomUserClaim> 
{
public ApplicationDbContext()
:base(DefaultConnection)
{
}

public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}

protected override void OnModelCreating(DbModelBinder modelBinder)
{
modelBinder.Entity< CustomRole>()ToTable(CustomRole); //这可能不需要,检查。

modelBinder.Entity< CustomRole>()。Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}

}


After following this article to "Change Primary Key for Users in ASP.NET Identity" I have an Issue when adding a new Role in Seed method.

Cannot insert the value NULL into column 'Id', table 'X.dbo.AspNetRoles'; column does not allow nulls. INSERT fails.
The statement has been terminated.

My code is:

protected override void Seed(ApplicationDbContext context)
{
    var roleStore = new CustomRoleStore(context);
    var roleManager = new ApplicationRoleManager(roleStore);

    if (!roleManager.RoleExists("Administrador"))
        roleManager.Create(new CustomRole("Administrador"));
}

And the error occurs on the last line: roleManager.Create...

Plus to the tutorial I have this implementation in IdentityConfig.cs:

public class ApplicationRoleManager : RoleManager<CustomRole, int>
{
    public ApplicationRoleManager(IRoleStore<CustomRole, int> roleStore)
        : base(roleStore) { }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        return new ApplicationRoleManager(new RoleStore<CustomRole, int, CustomUserRole>(context.Get<ApplicationDbContext>()));
    }
}

Edit 1

public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

解决方案

I can give some work around for this. You can manually update your CustomRole table on database to have auto increment values. Then override the EF OnModelCreating as explains below.

public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    protected override void OnModelCreating(DbModelBinder modelBinder)
    {
        modelBinder.Entity<CustomRole>().ToTable("CustomRole"); // This may be not needed, check it.

        modelBinder.Entity<CustomRole>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }

}

这篇关于将更改后的种子方法中的新角色添加到字符串到int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆