title: DBContext - Grid issue Many to Many relationship
body: <p>What I am trying to do is setup a User-Role-Permission table structure using entity framework.  It seems that the tables are being created correctly but when I try to display the roles in a grid(devexpress) the data repeats itself:</p>
  
  <p>So the first line in the grid shows the role.  If you then expand the role then all the Permissions will show under that role... then if you expand the permissions the role again will display under the permissions... then if you expand the role the permissions will show again... and so on.</p>
  
  <p>Role1</p>
  
  <p>--Permission1</p>
  
  <p>--Permission2</p>
  
  <p>----Role1</p>
  
  <p>------Permission1</p>
  
  <p>------Permission2</p>
  
  <p>-----------Role1</p>
  
  <p>etc...</p>
  
  <pre><code>Public Class User
      Inherits BaseTable
  
      &lt;Required&gt; _
      &lt;StringLength(20)&gt; _
      Public Property UserName As String = String.Empty
      &lt;StringLength(50)&gt; _
      Public Property FirstName As String = String.Empty
      &lt;StringLength(50)&gt; _
      Public Property LastName As String = String.Empty
      &lt;StringLength(20)&gt; _
      Public Property Password As String = String.Empty
      &lt;StringLength(100)&gt; _
      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
  
      &lt;StringLength(50)&gt; _
      Public Property RoleName As String = String.Empty
      &lt;StringLength(500)&gt; _
      Public Property RoleDescription As String = String.Empty
  
      Public Overridable Property Permissions As List(Of Permission)
  End Class
  
  Public Class Permission
      Inherits BaseTable
  
      &lt;Required&gt; _
      &lt;StringLength(50)&gt; _
      Public Property PermName As String = String.Empty
  
      Public Overridable Property Roles As List(Of BO.Table.Role)
  End Class
  
  Public Class BaseTable
      &lt;Key&gt; _
      &lt;Column(Order:=1)&gt; _
      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
  </code></pre>
  
  <p>I have an idea why it is displaying or grabbing the data like this but not totally sure how to fix it using entity framework.</p>
  
  <p>In the end my tables are:</p>
  
  <ol>
  <li>User is assigned role(s)</li>
  <li>Role is assigned permission(s)
  etc...</li>
  </ol>
  
  <p>User - Roles - UserRoles - Permission - RolePermission</p>
  
  <p>Hope this explains it.</p>