【问题标题】:Is there a way to create a custom User and Role without specifying the TKey on IdenitityUser, IdentityRole, and IdentityDbContext?有没有在 IdenitityUser、IdentityRole 和 IdentityDbContext 上指定 TKey 的情况下创建自定义用户和角色的方法?
【发布时间】:2014-05-08 15:06:32
【问题描述】:

有没有一种方法可以在不指定 IdentityUser、IdentityRole 和 IdentityDbContext 中的 TKey string 的情况下创建自定义用户和角色?我问是因为它似乎认为我不再想要自动生成的主键Id,我绝对想要。执行我在下面所做的操作,UserManager.Create(user, password) 将在 Id 上出现 EntityValidationError 失败。

public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }
}

public class ApplicationUserLogin : IdentityUserLogin
{
}

public class ApplicationUserClaim : IdentityUserClaim
{
}

public class ApplicationUserRole : IdentityUserRole
{
}

public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
    [Required]
    [StringLength(50)]
    public string ProperName { get; set; }

    [Required]
    public string Description { get; set; }
}


public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public MyAppDb()
        : base("MyAppDb")
    {
    }

    public static MyAppDb Create()
    {
        return new MyAppDb();
    }
}

【问题讨论】:

    标签: asp.net asp.net-mvc entity-framework asp.net-mvc-5 asp.net-identity


    【解决方案1】:

    我仍在学习这个新的 ASP.NET 身份系统,但您是否尝试过省略这样的泛型类型:

    public class ApplicationUser : IdentityUser
    {
        [Required]
        [StringLength(50)]
        public string FirstName { get; set; }
    
        [Required]
        [StringLength(50)]
        public string LastName { get; set; }
    }
    

    ApplicationRole 也是如此

    MyAppDb 看起来像这样:

    public class MyAppDb: IdentityDbContext<ApplicationUser>
    

    默认情况下,ID 将是带有数据库中自动生成的 GUID 的字符串

    【讨论】:

    • 我创建了一个自定义角色ApplicationRole,您必须在MyAppDb: IdentityDbContext&lt;...&gt; 中指定它。
    • 所以我尝试了你的建议,但必须像这样创建MyAppDb:public class GEOdaisiaMembershipDb : IdentityDbContext&lt;ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim&gt; 并在注册用户时在这行代码var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); 上收到此错误The entity type IdentityRole is not part of the model for the current context.。
    • 看看这个thread
    • 你需要将它添加到上下文public DbSet&lt;ApplicationRole&gt; Roles { get; set; }
    • 我查看了您在上面发送的那篇帖子,但没有帮助。我正在使用 AspNet.Identity2.0。 AspNetRoles 表存在。我已经创建了一个成功创建角色的控制器和视图。我在数据库中看到它们。
    【解决方案2】:

    答案似乎是“否”。如果您在 User 和/或 Role 上指定 TKey,则不再为您创建主键。

    似乎我试图使事情变得过于复杂。感谢@dima 帮助我决定让事情变得简单。以下是我为成功使用户和角色(都具有自定义属性)成功工作所做的工作,即通过控制器和视图成功地在数据库中创建记录:

    更新:您可能需要查看我在底部提供的链接以获得更好/更简单的解决方案。

    public class ApplicationUser : IdentityUser
    {
        [Required]
        [StringLength(50)]
        public string FirstName { get; set; }
    
        [Required]
        [StringLength(50)]
        public string LastName { get; set; }
    }
    
    //public class ApplicationUserLogin : IdentityUserLogin
    //{
    //}
    
    //public class ApplicationUserClaim : IdentityUserClaim
    //{
    //}
    
    //public class ApplicationUserRole : IdentityUserRole
    //{
    //}
    
    public class ApplicationRole : IdentityRole
    {
        [Required]
        [StringLength(50)]
        public string ProperName { get; set; }
    }
    
    
    public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
    {
        public MyAppDb()
            : base("MyAppDb")
        {
        }
    }
    

    但是,UserManager 出现了一个新问题。具体来说,我在这行代码var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); 上收到错误The entity type IdentityRole is not part of the model for the current context.

    更新:在此处修复此错误:Why am I getting an IdentityRole error?

    【讨论】:

      猜你喜欢
      • 2019-04-29
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多