这是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引用为具有多对一关系的外键?
答案 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:
从One
到Zore
到AspNetRoles
,例如:
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到One
到Many
:
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; }
}