C# 将列表放在自己的表中
我正试图在我的C#应用程序中建立一个权限系统 我的想法是:C# 将列表放在自己的表中,c#,entity-framework-core,C#,Entity Framework Core,我正试图在我的C#应用程序中建立一个权限系统 我的想法是: Class: Permission - string name; - string value; Class: Role - string name - List<Permission> Permissions; Class: User - List<Role> Roles; - List<Permission> AdditionalPermissions; 我可以让实体框架生成如下
Class: Permission
- string name;
- string value;
Class: Role
- string name
- List<Permission> Permissions;
Class: User
- List<Role> Roles;
- List<Permission> AdditionalPermissions;
我可以让实体框架生成如下结构,而不必自己创建C#类吗
Table: Permissions
- int id;
- nvarchar name;
- nvarchar value;
Table: Roles
- int id;
- nvarchar name;
Table: Users;
- int id
Table: Users_Permissions
- int UserID;
- int PermissionID;
Table: Role_Permissions
- int RoleID
- int PermissionID;
当您像这样构造实体类时,db结构会按照您想要的方式进行:
public class Permission
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
public class RolePermission
{
[ForeignKey(nameof(Role))]
public int RoleId { get; set; }
public virtual Role Role { get; set; }
[ForeignKey(nameof(Permission))]
public int PermissionId { get; set; }
public virtual Permission Permission { get; set; }
}
public class Role
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<RolePermission> RolePermissions { get; set; }
}
当您像这样构造实体类时,db结构会按照您想要的方式进行:
public class Permission
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
public class RolePermission
{
[ForeignKey(nameof(Role))]
public int RoleId { get; set; }
public virtual Role Role { get; set; }
[ForeignKey(nameof(Permission))]
public int PermissionId { get; set; }
public virtual Permission Permission { get; set; }
}
public class Role
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<RolePermission> RolePermissions { get; set; }
}
首先是数据库还是代码?首先是数据库还是代码?首先是代码这正是我写的,你非常快!回答得好!好吧,我本来希望我可以避免额外的课程,但我想,我不得不忍住了:P谢谢!那正是我写的,你跑得很快!回答得好!好吧,我本来希望我可以避免额外的课程,但我想,我不得不忍住了:P谢谢!
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<RolePermission>().HasKey(table => new {
table.RoleId,
table.PermissionId
});
}