我要做的是使用实体框架设置用户角色权限表结构。似乎正在正确创建表,但是当我尝试在网格中显示角色(devexpress)时,数据会自行重复:
因此,网格中的第一行显示了该角色。如果您随后展开角色,则所有权限将显示在该角色下...然后,如果您展开权限,角色将再次显示在权限下...然后,如果您展开角色,权限将再次显示...和等等。
基于role1
- 的外国人,
- Permission2
----基于role1
------的外国人,
------ Permission2
-----------基于role1
等...
Public Class User
Inherits BaseTable
<Required> _
<StringLength(20)> _
Public Property UserName As String = String.Empty
<StringLength(50)> _
Public Property FirstName As String = String.Empty
<StringLength(50)> _
Public Property LastName As String = String.Empty
<StringLength(20)> _
Public Property Password As String = String.Empty
<StringLength(100)> _
Public Property Email As String = String.Empty
Public Property isActive As Boolean = False
Public Overridable Property Roles As List(Of BO.Table.Role)
End Class
Public Class Role
Inherits BaseTable
<StringLength(50)> _
Public Property RoleName As String = String.Empty
<StringLength(500)> _
Public Property RoleDescription As String = String.Empty
Public Overridable Property Permissions As List(Of Permission)
End Class
Public Class Permission
Inherits BaseTable
<Required> _
<StringLength(50)> _
Public Property PermName As String = String.Empty
Public Overridable Property Roles As List(Of BO.Table.Role)
End Class
Public Class BaseTable
<Key> _
<Column(Order:=1)> _
Public Property ID As Integer = 0
End Class
Public Class UserContext
Inherits DbContext
Property Users As DbSet(Of BO.Table.User)
Property Roles As DbSet(Of BO.Table.Role)
Property Permissions As DbSet(Of BO.Table.Permission)
Public Sub New()
Entity.Database.SetInitializer(Of UserContext)(New UserInitializer)
End Sub
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
modelBuilder.Conventions.Remove(Of PluralizingTableNameConvention)()
modelBuilder.Entity(Of BO.Table.User)(). _
Property(Function(p) p.UserName).
HasColumnAnnotation("Index", New IndexAnnotation(New IndexAttribute With {.IsUnique = True}))
modelBuilder.Entity(Of BO.Table.Permission)(). _
Property(Function(p) p.PermName).
HasColumnAnnotation("Index", New IndexAnnotation(New IndexAttribute With {.IsUnique = True}))
modelBuilder.Entity(Of BO.Table.Role)(). _
Property(Function(p) p.RoleName).
HasColumnAnnotation("Index", New IndexAnnotation(New IndexAttribute With {.IsUnique = True}))
modelBuilder.Entity(Of BO.Table.User)().HasMany(Function(p) p.Roles).WithMany().
Map(Sub(m)
m.ToTable("UserRole")
m.MapLeftKey("UserID")
m.MapRightKey("RoleID")
End Sub)
modelBuilder.Entity(Of BO.Table.Role)().HasMany(Function(p) p.Permissions).WithMany(Function(pp) pp.Roles).
Map(Sub(m)
m.ToTable("RolePermission")
m.MapLeftKey("RoleID")
m.MapRightKey("PermissionID")
End Sub)
MyBase.OnModelCreating(modelBuilder)
End Sub
End Class
Public Class User
Private _db As UserContext
Public Sub New()
_db = New UserContext
End Sub
Public Function GetRoles() As List(Of BO.Table.Role)
Try
Return _db.Roles.ToList
Catch ex As Exception
Logger.log(String.Format("DAL.Role.GetRoles|{0}", ex.ToString))
Throw ex
End Try
End Function
End Class
我知道为什么它会显示或抓取这样的数据,但不完全确定如何使用实体框架来修复它。
最后我的表是:
用户 - 角色 - UserRoles - 权限 - RolePermission
希望这能解释它。
答案 0 :(得分:0)
我能够通过从权限类中删除以下行来解决我的问题:
Public Overridable Property Roles As List(Of BO.Table.Role)
然后在我的用户上下文中,我按如下方式调整了RolePermission的关系:
modelBuilder.Entity(Of BO.Table.Role)().HasMany(Function(p) p.Permissions).WithMany().
Map(Sub(m)
m.ToTable("RolePermission")
m.MapLeftKey("RoleID")
m.MapRightKey("PermissionID")
End Sub)
进行这两项更改后,网格现在可以正常工作。