3

I wrote a custom DataSet (based on strongly-typed DataSet, the further I see) and I want to make it suitable to be used on design-time, things like be dragged from ToolBox (generated automatically as a DataSet tool). All of this it's happening 'cause I'm trying to keep the development methodology, set by my workmates (I'm the new one, hahaha), Of course I'm aware of existence of Entity Framework and that kind of things. So, I'm getting this error when I place it on my form:

Relation named 'ParentChild_Relation' already belongs to this DataSet

Of course it's a matter relationship

This is the code:

Public Class MyDataSet
    Inherits DataSet

    Public Const PARENTCHILD_RELATION As String = "ParentChild_Relation"

    Public Sub New()
        MyBase.New()
        Me.BeginInit()
        Me.CreateDataSet()
        Me.EndInit()
    End Sub

    Private Sub CreateDataSet()
        DataSetName = "Test"
        Me.EnforceConstraints = True
        Me.Tables.Add(New ParentTable) 'custom datatable
        Me.Tables.Add(New ChildTable) 'custom datatable

        CreateRelation()
    End Sub

    Private Sub CreateRelation()
        With DirectCast(Me.Tables(ChildTable.TABLENAME).Constraints.Add(PARENTCHILD_RELATION ,
                                                                Me.Tables(ParentTable.TABLENAME).Columns(ParentTable.ID),
                                                                Me.Tables(ChildTable.TABLENAME).Columns(ChildTable.ID)), ForeignKeyConstraint)
            .UpdateRule = Rule.Cascade
            .DeleteRule = Rule.Cascade
            Me.Relations.Add(.ConstraintName, .RelatedColumns, .Columns, False)
        End With
    End Sub

End Class

I'll be aware of any possible help

EDIT

It works without this line:

Me.Relations.Add(.ConstraintName, .RelatedColumns, .Columns, False)

But, I still want the DataSet relation because is the way they work

EDIT 2

Without the previous line, it doesn't find relation through bindings:

BindingSource.DataSource = New MyDataSet
BindingSource.DataMember = "ParentChild_Relation"
Pedro Mora
  • 95
  • 9

1 Answers1

1

This I had to do: Implement the DataSet.Initialized event and then create all relationships within it.

Public Class MyDataSet
    Inherits DataSet

    Public Const PARENTCHILD_RELATION As String = "ParentChild_Relation"

    Public Sub New()
        MyBase.New()
        Me.CreateDataSet()  
        With DirectCast(Me.Tables(ChildTable.TABLENAME).Constraints.Add(PARENTCHILD_RELATION ,
                                                                Me.Tables(ParentTable.TABLENAME).Columns(ParentTable.ID),
                                                                Me.Tables(ChildTable.TABLENAME).Columns(ChildTable.ID)), ForeignKeyConstraint)
            .UpdateRule = Rule.Cascade
            .DeleteRule = Rule.Cascade
        End With
        AddHandler Initialized, AddressOf InitializedHandler
    End Sub        


    Private Sub InitializedHandler(sender As Object, e As EventArgs)            
        Relations.AddRange((From tb As DataTable In Tables
                        From fkc As ForeignKeyConstraint In tb.Constraints.OfType(Of ForeignKeyConstraint)()
                        Where Not Relations.Contains(fkc.ConstraintName)
                        Select New DataRelation(fkc.ConstraintName, fkc.RelatedColumns, fkc.Columns, False)).ToArray())
    End Sub

    Private Sub CreateDataSet()
        DataSetName = "Test"
        Me.EnforceConstraints = True
        Me.Tables.Add(New ParentTable) 'custom datatable
        Me.Tables.Add(New ChildTable) 'custom datatable
    End Sub

End Class

I thought it would be helpful for someone else

Pedro Mora
  • 95
  • 9