asp.net核心身份RoleTable的RoleId作为外键

时间:2018-10-09 09:32:17

标签: c# identity asp.net-core-mvc-2.0

这是asp.net核心身份角色表结构。

    AspNetRoles
 -------------------
-id (PrimaryKey)
-ConcurrencyStamp
-Name
-NormalizedName

我想创建一个新表,其中包括该角色表的id作为外键。
这是我的新桌子poco

  public class NewClass
{
    public string Name { get; set; }

    public int Type { get; set; }

    //
}

如何将AspNetRoles的ID引用为具有多对一关系的外键?

3 个答案:

答案 0 :(得分:0)

应该是这样的

public class AspNetRoles
{
    public int RoleId { get; set; }
    public string ConcurrencyStamp { get; set; }
    public string Name { get; set; }
    public string NormalizedName{ get; set; }

    public List<NewClass> NewClasses{ get; set; }
}

public class NewClass
{
   public string Name { get; set; }

   public int Type { get; set; }

   public int RoleId { get; set; }
   public AspNetRoles AspNetRole{ get; set; }
}

答案 1 :(得分:0)

我有点困惑,哪一方是你的很多,哪一侧是你的很多。 如果您使用的是实体框架,那么请尝试以下示例。实体框架足够智能,可以在它们之间建立一对多的关系

如果您有很多方面是NewClass,请尝试使用此方法。或者,如果您需要在NewClass中担任多个角色

    public class AspNetRole
    {
      public int RoleId { get; set; }    
      public string Name { get; set; }     
    }



   public class NewClass
   {
    ..................
    ................

    public ICollection<AspNetRole> AspNetRoles{ get; set; }
   }

如果您的“多面手是角色”,请尝试使用此

    public class AspNetRole
   {
      public int RoleId { get; set; }    
      public string Name { get; set; }  
      public ICollection<NewClass> NewClasses{ get; set; }   
    }



  public class NewClass
  {
   ................

    public int AspNetRolesId{ get; set; }
    public AspNetRole AspNetRoles{ get; set; }
  }

答案 2 :(得分:0)

您可以执行以下操作:

OneToZore:

OneZoreAspNetRoles,例如:

Public class AspNetRole
{
    //Other property
    public int NewClassId { get; set; }
}

public class NewClass
{
    //Other property
    public AspNetRole AspNetRole { get; set; }
}

NewClass到Zore:

Public class AspNetRole
{
    //Other property
    public NewClass NewClass { get; set; }
}

public class NewClass
{
    //Other property
    public int AspNetRoleId { get; set; }
}

一对多:

从AspNetRole将One转换为Many

Public class AspNetRole
{
    //Other property
    public int NewClassId { get; set; }
}

public class NewClass
{
    //Other property
    public virtual ICollection<AspNetRole> AspNetRoles { get; set; }
}

对于从NewClass到OneMany

Public class AspNetRole
{
    //Other property
    public virtual ICollection<NewClass> NewClasses { get; set; }
}

public class NewClass
{
    //Other property
    public int AspNetRoleId { get; set; }
}

多对多:

对于此问题,您不能直接创建该表,必须找到与该表有关系的表。例如,User表与AspNetRole的关系(一对多)如下:

Public class AspNetRole
{
    //Other property
    public int UserId { get; set; }
}

public class User
{
    //Other property
    public virtual ICollection<AspNetRole> AspNetRoles { get; set; }
}

现在,如果您与OneToMany表之间的关系为User,则之后与AspNetRole的关系为ManyToMany,例如:

Public class AspNetRole
{
    //Other property
    public int UserId { get; set; }
}

public class User
{
    //Other property
    public virtual ICollection<AspNetRole> AspNetRoles { get; set; }
    public virtual ICollection<NewClass> NewClasses { get; set; }
}

public class NewClass
{
    //Other property
    public int AspNetRoleId { get; set; }
}