【问题标题】:DBContext - Grid issue Many to Many relationshipDBContext - 网格问题多对多关系
【发布时间】:2015-05-29 07:19:11
【问题描述】:

我要做的是使用实体框架设置用户角色权限表结构。似乎正在正确创建表格,但是当我尝试在网格(devexpress)中显示角色时,数据会重复自身:

所以网格中的第一行显示了角色。如果您随后展开角色,则所有权限将显示在该角色下......然后如果您展开权限,角色将再次显示在权限下......然后如果您展开角色,权限将再次显示......并且以此类推。

角色1

--权限1

--权限2

----角色1

-----权限1

-----权限2

-----------角色1

等等……

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

我知道它为什么会这样显示或抓取数据,但不完全确定如何使用实体框架来修复它。

最后我的表格是:

  1. 为用户分配了角色
  2. 角色已分配权限 等等……

用户 - 角色 - 用户角色 - 权限 - 角色权限

希望这能解释清楚。

【问题讨论】:

  • 不要从dbContext继承。要添加更多功能,请使用Partial Class。不要认为这会解决你的问题,但它是一个更好的开始方法。

标签: c# vb.net entity-framework dbcontext


【解决方案1】:

我能够通过从权限类中删除以下行来解决我的问题:

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)

进行这两项更改后,网格现在似乎可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-14
    • 2015-07-09
    • 2014-11-11
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多