C# DBContext-网格问题多对多关系

C# DBContext-网格问题多对多关系,c#,vb.net,entity-framework,dbcontext,C#,Vb.net,Entity Framework,Dbcontext,我正在尝试使用实体框架设置用户角色权限表结构。表的创建似乎是正确的,但当我尝试在网格(devexpress)中显示角色时,数据会自动重复: 因此,网格中的第一行显示了角色。如果随后展开该角色,则该角色下将显示所有权限。。。然后,如果您再次展开权限,角色将显示在权限下。。。然后,如果您展开角色,权限将再次显示。。。等等 角色1 --许可证1 --许可证2 ----角色1 ------许可证1 ------许可证2 -----------角色1 等等 Public Class User In

我正在尝试使用实体框架设置用户角色权限表结构。表的创建似乎是正确的,但当我尝试在网格(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
公共类用户
继承基表
_
_
公共属性用户名为String=String.Empty
_
公共属性名为String=String.Empty
_
公共属性LastName为String=String.Empty
_
公共属性密码为String=String.Empty
_
公共属性电子邮件为String=String.Empty
公共属性isActive为布尔值=False
作为列表的公共可重写属性角色(BO.Table.Role的)
末级
公共阶级角色
继承基表
_
公共属性RoleName As String=String.Empty
_
公共属性角色描述为String=String.Empty
公共可重写属性权限作为(权限的)列表
末级
公共类许可
继承基表
_
_
公共属性PermName As String=String.Empty
作为列表的公共可重写属性角色(BO.Table.Role的)
末级
公共类基表
_
_
公共属性ID为整数=0
末级
公共类用户上下文
继承DbContext
属性用户作为DbSet(属于BO.Table.User)
作为DbSet的属性角色(属于BO.Table.Role)
作为DbSet的属性权限(BO.Table.Permission的)
公共分新()
Entity.Database.SetInitializer(属于UserContext)(新的UserInitializer)
端接头
模型创建时受保护的覆盖子项(modelBuilder作为DbModelBuilder)
modelBuilder.Conventions.Remove(对于PluralizingTableNameConvention)()
实体(BO.Table.User的)()_
属性(函数(p)p.UserName)。
HasColumnAnnotation(“索引”,新的IndexAnnotation(带有{.IsUnique=True}的新IndexAttribute))
实体(BO.Table.Permission的)()_
属性(函数(p)p.PermName)。
HasColumnAnnotation(“索引”,新的IndexAnnotation(带有{.IsUnique=True}的新IndexAttribute))
实体(BO.Table.Role的)()_
属性(函数(p)p.RoleName)。
HasColumnAnnotation(“索引”,新的IndexAnnotation(带有{.IsUnique=True}的新IndexAttribute))
实体(BO.Table.User的)()。有许多(函数(p)p.Roles)。WithMany()。
地图(Sub(m)
m、 ToTable(“用户角色”)
m、 MapLeftKey(“用户ID”)
m、 MapRightKey(“RoleID”)
末端接头)
实体(BO.Table.Role的)()。有许多(函数(p)p.Permissions)。有许多(函数(p)p.Roles)。
地图(Sub(m)
m、 ToTable(“角色许可”)
m、 MapLeftKey(“RoleID”)
m、 MapRightKey(“许可ID”)
末端接头)
MyBase.OnModelCreating(modelBuilder)
端接头
末级
公共类用户
私有数据库作为用户上下文
公共分新()
_db=新用户上下文
端接头
公共函数GetRoles()作为列表(BO.Table.Role的列表)
尝试
Return\u db.Roles.ToList
特例
log(String.Format(“DAL.Role.GetRoles |{0}”,ex.ToString))
投手
结束尝试
端函数
末级
我知道为什么它会像这样显示或抓取数据,但不完全确定如何使用实体框架修复它

最后,我的表格是:

  • 已为用户分配角色
  • 角色已分配权限 等等
  • 用户-角色-用户角色-权限-角色权限


    希望这能解释问题。

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

    Public Overridable Property Roles As List(Of BO.Table.Role)
    
    然后在我的用户上下文中,我调整了角色权限的关系,如下所示:

    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)
    

    完成这两项更改后,网格现在似乎可以工作。

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