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

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

问题描述

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

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

在控制器中,如下所示:

And in the controller like this:

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; }

    ...
}

ApplicationUser ApplicationRole ApplicationUserLogin ApplicationUserRole ApplicationUserClaim 都分别从Identity类继承。

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

任何人看到任何错误?在我创建了所有没有 Id的自定义标识类之前,我可以注册用户( UserManager.Create())问题。

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

更新:似乎为密钥指定了 string - 生成键入数据库上下文。这是我最好的猜测。现在正在寻找我设置 Id 。我不想,我希望它在我在用户之前创建自定义字段时所做的工作。

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?

-OR -

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

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

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

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