gpt4 book ai didi

asp.net-mvc - UserManager.Create(用户,密码)抛出 EntityValidationError 说 Id 是必需的?

转载 作者:行者123 更新时间:2023-12-02 06:01:50 30 4
gpt4 key购买 nike

任何人都知道为什么 UserManager.Create(user, password); 可能会抛出 EntityValidationErrorId 是必需的。

这可能与我的UserManager有关吗?设置如下:

public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public static MyAppDb Create()
{
return new MyAppDb();
}

public MyAppDb()
: base("MyAppDb")
{
}
}


public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public ApplicationUserStore(MyAppDb context)
: base(context)
{
}
}

public class ApplicationUserManager : UserManager<ApplicationUser, string>
{
public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
: base(store)
{
}
}

在 Controller 中像这样:

public class AccountController : Controller
{
public AccountController()
: this(new ApplicationUserManager(new ApplicationUserStore(new MyAppDb())))
{
}

public AccountController(ApplicationUserManager userManager)
{
UserManager = userManager;
UserManager.PasswordValidator = (IIdentityValidator<string>)new MinimumLengthValidator(8);
UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true };
}

public ApplicationUserManager UserManager { get; private set; }

...
}

ApplicationUserApplicationRoleApplicationUserLoginApplicationUserRoleApplicationUserClaim 均继承分别来自 Identity 类。

有人发现有什么问题吗?在创建所有自定义 Identity 类之前,我能够注册用户 (UserManager.Create()),而不会出现 Id 问题。

更新:似乎为 key 指定字符串已经破坏了数据库上下文中自动生成的 key 。这是我最好的猜测。它现在正在寻找我设置Id。我不想,我希望它按照在 User 上创建自定义字段之前的方式工作。

最佳答案

指定字符串似乎告诉UserStore您将提供Id。当我解决以下问题时,这个问题就消失了 Is there a way to create a custom User and Role without specifying the TKey on IdenitityUser, IdentityRole, and IdentityDbContext?

-或-

只需给它一个id,如下所示:Guid.NewGuid().ToString()

关于asp.net-mvc - UserManager.Create(用户,密码)抛出 EntityValidationError 说 Id 是必需的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22698744/

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