UserManager.Create(user, password) 抛出 EntityValidationError 说 Id 是必需的? [英] UserManager.Create(user, password) thowing EntityValidationError saying Id is required?

查看:21
本文介绍了UserManager.Create(user, password) 抛出 EntityValidationError 说 Id 是必需的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Anybody know why UserManager.Create(user, password); might be throwing EntityValidationError saying Id is required.

会不会和我的UserManager有关.设置如下:

Could it have something to do with my UserManager. It is setup like this:

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)
    {
    }
}

在这样的控制器中:

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 类.

ApplicationUser, ApplicationRole, ApplicationUserLogin, ApplicationUserRole, and ApplicationUserClaim all inherit from the Identity classes respectively.

有人看到有什么不对吗?在创建所有自定义身份类之前,我能够注册用户 (UserManager.Create()),而没有 Id 问题.

Anyone see anything wrong? I was able to register users (UserManager.Create()) before I created all the custom Identity classes without the Id issue.

更新: 似乎为 Key 指定 string 已经破坏了数据库上下文中自动生成的 Key.这是我最好的猜测.现在正在找我设置Id.我不想,我希望它像我在 User 上创建自定义字段之前那样工作.

UPDATE: It seems specifying string for the Key has broken the auto-generated Key in the database context. That is my best guess. It now is looking for me to set the Id. I don't want to, I want it to work the way it did before I created the custom fields on the User.

推荐答案

它似乎指定 string 告诉 UserStore 你将提供 Id>.当我解决了以下问题时,这个问题就消失了 是否可以在不指定 IdenitityUser、IdentityRole 和 IdentityDbContext 上的 TKey 的情况下创建自定义用户和角色?

It appears specifying string tells the UserStore that you will provide the Id. This issue went away when I solved the following issue Is there a way to create a custom User and Role without specifying the TKey on IdenitityUser, IdentityRole, and IdentityDbContext?

-或-

只需给它一个 id 像这样:Guid.NewGuid().ToString()

Just give it an id like this: Guid.NewGuid().ToString()

这篇关于UserManager.Create(user, password) 抛出 EntityValidationError 说 Id 是必需的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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