2 DataGridView with cascade update and ADO.NET Entity Data Model

81 Views Asked by At

I try to find a solution in this link Cascade update with TableAdapterManager with Dataset and TableAdapterManager, but there are no answers, then I’m trying to do it with Entity Framework.

I have a SQL Server database with 2 tables and a relation with these properties: SQL Server Database diagram

Fill the tables with sample data. Then I create a new Windows forms application Visual Basic (.Net Framework) project. Add a new element ADO.NET Entity Data Model named DataBaseMD. Connect to the previous database and add the 2 tables Master and Detail, compile de project and add the data source.

enter image description here

Drag and drop to the form1 from Master data source and then for Detail data source under Master. enter image description here

First question is why Detail DataGridView is not recognizing the 2 fields and either the relationship and not showing information.

I modified the Detail binding source and try to manage in the CurrentChanged event of the Master Binding Source to show the information in the Detail DataGridView. When I change a Master item it needs to show the details of that item selected: enter image description here

Imports System.Data.Entity
Public Class Form1
    Public db As New DataBaseMD
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        db.Master.Load
        MasterBindingSource.DataSource = db.Master.Local.ToBindingList
    End Sub
    Private Sub MasterBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles MasterBindingNavigatorSaveItem.Click
        db.SaveChanges()
    End Sub
    Private Sub MasterBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles MasterBindingSource.CurrentChanged
        Dim m As Master = Me.MasterBindingSource.Current
        db.Detail.Where(Function(p) p.id = m.id).Load
        DetailBindingSource.DataSource = db.Detail.Local.ToBindingList
        DetailBindingSource.Filter = "id = " & m.id
    End Sub
End Class

With this code the master and detail DataGridViews are working with updates, insert and delete items and with the SaveChanges enabled, but when changing the master item, the old item persist showing in details, the filter is not working, and it shows more and more when changing the master item.

If I update a key in Master table (id), the cascade update is not work showing this error:

System.InvalidOperationException: 'The property 'id' is part of the object's key information and cannot be modified. '

Are there a better way to do the 2 DataGridViews with cascade update linked to SQL Server database, better than DataSets or ADO.NET Entity Data Model?

Edited: I found this article with the problem of details, but I don't understand how to change ICollection<Detail> to bindingList<Detail>

I create this video using DataSet, the master-detail shows ok, but cascade delete update, do not work. Using_DataSet_Video

Then I try using ADO.Net Entity data model, but now no master-detail nither cascade update works. Using_ADO.Net_Entity_data_Model_Video

0

There are 0 best solutions below