如何从IdentityUserLogin和IdentityUserRole asp.net mvc继承

时间:2017-11-16 10:15:45

标签: c# .net asp.net-mvc entity-framework asp.net-identity

我有两个类,他们是名字tblUserProfile和tblUserRole我想在我提到的我自己的类中添加多个属性。事实上,tblUserProfile使用tblUserProfile:IdentityUserLogin而tblUserRole使用tblAccessRole : IdentityUserRole但是当我做的时候添加迁移它确实给了我这个错误EntityType has no key defined. Define the key for this EntityType 之后我添加了关键属性,然后EF确实显示了Column names in each table must be unique. Column name 'RoleId' in table 'tblAccessRoles' is specified more than once.,请告诉我如何解决它。

   public class tblUserProfile : IdentityUserLogin
    {
        public int UserID { get; set; }
        public string NameFamily { get; set; }

    }

  public class tblAccessRole : IdentityUserRole
    {

        public int RoleID { get; set; }
        public string  Section { get; set; }
        public bool IsSave { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

UserID移除tblUserProfile,从RoleID移除tblAccessRole

在您的DbContext中添加:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

      modelBuilder.Entity<tblUserProfile>()//IdentityUserLogin
            .HasKey(l => new { l.LoginProvider, l.ProviderKey, l.UserId })
            .ToTable("tblUserProfile");// Database Table Nam    

            modelBuilder.Entity<tblAccessRole>()//IdentityUserRole
            .HasKey(r => new { r.UserId, r.RoleId })
                .ToTable("tblAccessRole");// Database Table Name
    }