错误:“Id字段是必需的”。同时使用AspNet.Identity 2.0在数据库中注册新用户 [英] Error: "The Id field is required." while registering new user in database with AspNet.Identity 2.0

查看:198
本文介绍了错误:“Id字段是必需的”。同时使用AspNet.Identity 2.0在数据库中注册新用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欢迎,

在我的Asp.Net mvc应用程序中创建(注册)新用户时,我收到主题中提到的验证错误,其中我使用Asp.Net Identity with我的自定义ApplicationUser类

I get the validation error mentioned in topic while creating (registering) new user in my Asp.Net mvc application where I used Asp.Net Identity with my custom ApplicationUser class

public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public async Task<ClaimsIdentity>
    GenerateUserIdentityAsync(ApplicationUserManager manager)
    {      
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);   
        return userIdentity;
    }

    public virtual string EmailConfirmedChar { get; set; }

    public virtual string PhoneNumberConfirmedChar { get; set; }

    public virtual string TwoFactorEnabledChar { get; set; }

    public virtual string LockoutEnabledChar { get; set; }
}

我的 TKey 是字符串,所以我希望 Id Guid ,因为它是默认的。

where my TKey is string, so I expect the Id to be Guid as it is by default.

当然,我实现了我的自定义UserStore,UserManager等,一般来说我灵感来自博客文章使用实体框架Fluent API自定义ASP.NET身份数据模型

Of course I implemented my custom UserStore, UserManager etc, in general I inspired by the blog post Customizing the ASP.NET Identity Data Model with the Entity Framework Fluent API

我做错了什么?
在下载此博客文章中附带的示例时,会出现相同的验证错误。

What do I do wrong ? Generaly the same validation error appears while downloaded the sample attached in this blog post.

相关或也不,我使用Firebird数据库。

Relevant or nor, I use Firebird database.

推荐答案

IdentityUser 当您创建新的实例时,类将自动生成GUID。 IdentityUser 类。
但是,您可以通过在 ApplicationUser 类构造函数中生成新的GUID来轻松克服此问题:

IdentityUser class automatically generates GUID when you make a new instance of it. Not IdentityUser<TKey, TLogin, TRole, TClaim> class. However you could easily overcome this problem by generating new GUID in your ApplicationUser class constructor:

public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationUser()
    {
        Id = Guid.NewGuid().ToString();
    }
    // rest of code
}

这篇关于错误:“Id字段是必需的”。同时使用AspNet.Identity 2.0在数据库中注册新用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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