Parent / Child Databinding Using Entity Framework 6.0

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi All,

I am having trouble with what I expected to be quite a simple function of Entity Framework 6.0. That is Parent / Child (Master / Detail) Databinding to controls using DataGridViews.

To explain my issue, here is a simple EDM that I am using generated from an SQL Server 2012 Database:-
EDM.JPG

As you can see, it has a Parent Table and a Child Table with a Relationship between ParentID.

When using a Dataset with two tables Parent / Child Databinding can be achieved by using a Parent BindingSource with its DataSource property set to the Parent Table and a Child BindingSource with its DataSource property set to the Parent BindingSource and it's DataMember property set the name of the Relationship between the two tables. So, knowing that, and rightly or wrongly, I am try to achieve the same solution using the Entity Framework by saying:-

Imports System.Data.Entity
 
Public Class Form1
  Private context As New EFTestEntities
  Private parentBS As New BindingSource
  Private childBS As New BindingSource
 
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    context.tblParentDetails.Load()
 
    parentBS.DataSource = context.tblParentDetails.Local.ToBindingList
    With childBS
      .DataSource = parentBS
      .DataMember = "tblChildDetails"
    End With
 
    ParentDGV.DataSource = parentBS
    ChildDGV.DataSource = childBS
  End Sub
End Class


As you can see when the Form Loads all looks fine. i.e:-
FormLoad.JPG

However, when I begin to navigate the Parent Table I then get this error:-
ParentChanged.JPG

After looking into this error, it seems like the Children being returned from the Association is changing and causing the said Type error described above. To try and confirm this, I added a message box to the RowEnter event of the Child DataGridView and can confirm that the number of child rows being returned is indeed changing from 4 to 1 and therefore what is actually being returned must also be changing causing the error.

Confused? I know I am but can anyone give any guidance as to what is actually happing and whether I am going about this all wrong with the Entity Framework.

Thanks in advance for any help you can give.

Kind regards,

Ian
 
Back
Top