.Net DataSet不适合设计时

时间:2016-04-13 18:05:17

标签: vb.net foreign-keys dataset design-time

我写了一个自定义DataSet(基于强类型DataSet,我看得越远),我想让它适合在设计时使用,比如从ToolBox拖动(自动生成为DataSet工具) 。所有这一切都在发生,因为我试图保持开发方法,由我的同事设定(我是新人,哈哈哈),当然我是意识到实体框架的存在和那种事情。所以,当我把它放在我的表单上时,我收到了这个错误:

  

名为' ParentChild_Relation'已经属于这个DataSet

当然,这是一种物质关系

这是代码:

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

我会发现任何可能的帮助

修改

没有这条线就可以了:

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

但是,我仍然需要DataSet关系,因为它们的工作方式

编辑2

如果没有上一行,它就不会通过绑定找到关系:

BindingSource.DataSource = New MyDataSet
BindingSource.DataMember = "ParentChild_Relation"

1 个答案:

答案 0 :(得分:1)

我必须这样做:实施 DataSet.Initialized 事件,然后在其中创建所有关系。

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

我认为这对其他人有帮助