您可以创建触发器而不是级联删除,例如(MS SQL-Server,未经测试):
CREATE TRIGGER [dbo].[trDeleteTableB] ON [dbo].[TableA]
FOR DELETE
AS
DELETE FROM TableB
WHERE (MessageID IN
(SELECT MessageID
FROM DELETED
WHERE (NOT EXISTS
(SELECT * FROM TableA
WHERE (TableA.MessageID = DELETED.MessageID)))))
另一个想法是扩展您的类型化数据集。如果要扩展功能,则不能更改 DatesetName.designer.cs/vb 中生成的类(任何更改都会重新创建),但名称中没有设计器的文件(如果不存在则创建它)。然后你必须扩展部分 DataTable 类(如果你不知道确切的名称,请查看设计器文件,它通常是 fe TableBDataTable)。
看看下面的代码,看看我的意思,我没有测试过,但我希望你明白我的意思:
Partial Class Datset1
Partial Class TableBDataTable
Private Sub TableB_RowDeleting(ByVal sender As Object, ByVal e As TableBRowChangeEvent) Handles Me.TableBRowDeleting
If e.Action = DataRowAction.Delete Then
'*** check here if there is another TableARow with this MessageID ***'
e.Row.RejectChanges()
End If
End Sub
End Class
End Class