【问题标题】:Add additional properties to IdentityUser ASP.NET MVC 5向 IdentityUser ASP.NET MVC 5 添加其他属性
【发布时间】:2016-12-12 17:12:28
【问题描述】:

用户表

 [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 表中也有其他字段

【问题讨论】:

    标签: c# asp.net-mvc ef-code-first asp.net-identity


    【解决方案1】:

    你需要将OnModelCreating中的行改成myContext

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

    到:

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

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

    【讨论】:

    • 谢谢你拯救了我的一天!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    相关资源
    最近更新 更多