将其他属性添加到Identityuser ASP.NET MVC 5中



用户表

 [Table("Users")]
   public  class User : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
        [Required]
        [Display(Name = "Name")]
        public string Name { get; set; }
        [Required]
        [Display(Name = "Gender")]
        public string Gender { get; set; }
        [Required]
        [Display(Name = "Date of birth")]
        public string DateOfBirth { get; set; }
    }

myContext类

 public class myContext : IdentityDbContext<User>
    {
        public myContext() : base("DefaultConnection")
        {
        }
        public static myContext Create()
        {
            return new myContext();
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");
            modelBuilder.Entity<IdentityRole>().ToTable("Roles", "dbo");
            modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles", "dbo");
            modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims", "dbo");
            modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins", "dbo");
        }
    }

这正在创建两个表

  1. aspnetusers-带有加法字段
  2. 用户 - 带有默认字段

请注意,我在这里缺少什么。我如何在Users表中也有其他字段

您需要更改myContext中的OnModelCreating中的行:

 modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");

to:

 modelBuilder.Entity<User>().ToTable("Users", "dbo");

现在,它正在创建两个表,因为您已经定义了IdentityUser类型用于创建名为Users的表,并且一个默认表正在针对IdentityUser类型创建。

相关内容

  • 没有找到相关文章

最新更新