错误:"Id字段是必需的." 使用AspNet.Identity 2.0在数据库中注册新用户时

rob*_*nwk 2 c# asp.net-mvc entity-framework-6 asp.net-identity

欢迎,

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

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; }
}
Run Code Online (Sandbox Code Playgroud)

在我的TKey是字符串,所以我希望IdGuid因为它是默认的.

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

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

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

Sam*_*ari 10

IdentityUserclass在创建新实例时会自动生成GUID. IdentityUser<TKey, TLogin, TRole, TClaim>上课.但是,您可以通过在ApplicationUser类构造函数中生成新的GUID来轻松解决此问题:

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