gpt4 book ai didi

asp.net - 有没有办法在不指定 IdenitityUser、IdentityRole 和 IdentityDbContext 上的 TKey 的情况下创建自定义用户和角色?

转载 作者:行者123 更新时间:2023-12-05 07:59:07 27 4
gpt4 key购买 nike

有没有办法在 IdentityUserIdentityRoleIdentityDbContext 中创建自定义用户和角色而不指定 TKey string ?我问是因为它似乎认为我不再想要自动生成的主键 Id 而我绝对想要。执行我在下面所做的操作时,UserManager.Create(user, password) 将失败,并在 Id 上出现 EntityValidationError

public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
[Required]
[StringLength(50)]
public string FirstName { get; set; }

[Required]
[StringLength(50)]
public string LastName { get; set; }
}

public class ApplicationUserLogin : IdentityUserLogin
{
}

public class ApplicationUserClaim : IdentityUserClaim
{
}

public class ApplicationUserRole : IdentityUserRole
{
}

public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
[Required]
[StringLength(50)]
public string ProperName { get; set; }

[Required]
public string Description { get; set; }
}


public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public MyAppDb()
: base("MyAppDb")
{
}

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

最佳答案

我仍在学习这个新的 ASP.NET Identity 系统,但您是否尝试过像这样省略泛型类型:

public class ApplicationUser : IdentityUser
{
[Required]
[StringLength(50)]
public string FirstName { get; set; }

[Required]
[StringLength(50)]
public string LastName { get; set; }
}

同样适用于 ApplicationRole

MyAppDb 看起来像这样:

public class MyAppDb: IdentityDbContext<ApplicationUser>

默认情况下,ID 将是数据库中自动生成的 GUID 的字符串

关于asp.net - 有没有办法在不指定 IdenitityUser、IdentityRole 和 IdentityDbContext 上的 TKey 的情况下创建自定义用户和角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22724938/

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