UserManager.Create(用户名,密码)异常触发EntityValidationError说ID是必需的? [英] UserManager.Create(user, password) thowing EntityValidationError saying Id is required?

查看:969
本文介绍了UserManager.Create(用户名,密码)异常触发EntityValidationError说ID是必需的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么 UserManager.Create(用户名,密码); 可能会被扔 EntityValidationError 标识是必需的。

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 所有分别从身份类继承。

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

任何人看到什么不对吗?我能注册用户( 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.

更新:看来指定字符串的重点已经打破了数据库上下文自动生成的密钥。这是我最好的猜测。现在是找我来设置编号。我不想,我希望它的工作,我创建的用户自定义字段之前做的那样

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.

推荐答案

它出现指定字符串通知 UserStore 你会提供编号。这个问题就走了,当我解决了以下问题<一href=\"http://stackoverflow.com/questions/22724938/is-there-a-way-to-create-a-custom-user-and-role-without-specifying-the-tkey-on-I\">Is有没有一种方法来创建自定义用户和角色,而无需指定有关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(用户名,密码)异常触发EntityValidationError说ID是必需的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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