This topic has not yet been rated - Rate this topic

ForeignKeyConstraint Class

Represents an action restriction enforced on a set of columns in a primary key/foreign key relationship when a value or row is either deleted or updated.

System.Object
  System.Data.Constraint
    System.Data.ForeignKeyConstraint

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)
public class ForeignKeyConstraint : Constraint

The ForeignKeyConstraint type exposes the following members.

  Name Description
Public method Supported by the XNA Framework ForeignKeyConstraint(DataColumn, DataColumn) Initializes a new instance of the ForeignKeyConstraint class with the specified parent and child DataColumn objects.
Public method Supported by the XNA Framework ForeignKeyConstraint(DataColumn[], DataColumn[]) Initializes a new instance of the ForeignKeyConstraint class with the specified arrays of parent and child DataColumn objects.
Public method Supported by the XNA Framework ForeignKeyConstraint(String, DataColumn, DataColumn) Initializes a new instance of the ForeignKeyConstraint class with the specified name, parent and child DataColumn objects.
Public method Supported by the XNA Framework ForeignKeyConstraint(String, DataColumn[], DataColumn[]) Initializes a new instance of the ForeignKeyConstraint class with the specified name, and arrays of parent and child DataColumn objects.
Public method Supported by the XNA Framework ForeignKeyConstraint(String, String, String[], String[], AcceptRejectRule, Rule, Rule) This constructor is provided for design time support in the Visual Studio environment. ForeignKeyConstraint objects created by using this constructor must then be added to the collection via AddRange. Tables and columns with the specified names must exist at the time the method is called, or if BeginInit has been called prior to calling this constructor, the tables and columns with the specified names must exist at the time that EndInit is called.
Public method Supported by the XNA Framework ForeignKeyConstraint(String, String, String, String[], String[], AcceptRejectRule, Rule, Rule) This constructor is provided for design time support in the Visual Studio environment. ForeignKeyConstraint objects created by using this constructor must then be added to the collection via AddRange. Tables and columns with the specified names must exist at the time the method is called, or if BeginInit has been called prior to calling this constructor, the tables and columns with the specified names must exist at the time that EndInit is called.
Top
  Name Description
Protected property Supported by the XNA Framework _DataSet Infrastructure. Gets the DataSet to which this constraint belongs. (Inherited from Constraint.)
Public property Supported by the XNA Framework AcceptRejectRule Indicates the action that should take place across this constraint when AcceptChanges is invoked.
Public property Supported by the XNA Framework Columns Gets the child columns of this constraint.
Public property Supported by the XNA Framework ConstraintName The name of a constraint in the ConstraintCollection. (Inherited from Constraint.)
Public property Supported by the XNA Framework DeleteRule Gets or sets the action that occurs across this constraint when a row is deleted.
Public property Supported by the XNA Framework ExtendedProperties Gets the collection of user-defined constraint properties. (Inherited from Constraint.)
Public property Supported by the XNA Framework RelatedColumns The parent columns of this constraint.
Public property Supported by the XNA Framework RelatedTable Gets the parent table of this constraint.
Public property Supported by the XNA Framework Table Gets the child table of this constraint. (Overrides Constraint.Table.)
Public property Supported by the XNA Framework UpdateRule Gets or sets the action that occurs across this constraint on when a row is updated.
Top
  Name Description
Protected method Supported by the XNA Framework CheckStateForProperty Infrastructure. Gets the DataSet to which this constraint belongs. (Inherited from Constraint.)
Public method Supported by the XNA Framework Equals Gets a value indicating whether the current ForeignKeyConstraint is identical to the specified object. (Overrides Object.Equals(Object).)
Protected method Supported by the XNA Framework Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA Framework GetHashCode Gets the hash code of this instance of the ForeignKeyConstraint object. (Overrides Object.GetHashCode().)
Public method Supported by the XNA Framework GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method Supported by the XNA Framework MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method Supported by the XNA Framework SetDataSet Sets the constraint's DataSet. (Inherited from Constraint.)
Public method Supported by the XNA Framework ToString Gets the ConstraintName, if there is one, as a string. (Inherited from Constraint.)
Top

A ForeignKeyConstraint restricts the action performed when a value in a column (or columns) is either deleted or updated. Such a constraint is intended to be used with primary key columns. In a parent/child relationship between two tables, deleting a value from the parent table can affect the child rows in one of the following ways.

  • The child rows can also be deleted (a cascading action).

  • The values in the child column (or columns) can be set to null values.

  • The values in the child column (or columns) can be set to default values.

  • An exception can be generated.

ForeignKeyConstraint objects are contained in the ConstraintCollection of a DataTable, which is accessed through the Constraints property.

Constraints are not enforced unless the EnforceConstraints property is set to true.

The AcceptRejectRule is enforced whenever a DataTable object's AcceptChanges method is invoked.

The following example creates a ForeignKeyConstraint, sets some of its properties, and adds it to a DataTable object's ConstraintCollection.


' The next line goes into the Declarations section of the module:
' SuppliersProducts is a class derived from DataSet.
Private suppliersProducts As SuppliersProducts

Private Sub CreateConstraint()
    ' Declare parent column and child column variables.
    Dim parentColumn As DataColumn
    Dim childColumn As DataColumn
    Dim fkeyConstraint As ForeignKeyConstraint

    ' Set parent and child column variables.
    parentColumn = suppliersProducts.Tables("Suppliers").Columns("SupplierID")
    childColumn = suppliersProducts.Tables("Products").Columns("SupplierID")
    fkeyConstraint = New ForeignKeyConstraint( _
        "SupplierFKConstraint", parentColumn, childColumn)

    ' Set null values when a value is deleted.
    fkeyConstraint.DeleteRule = Rule.SetNull
    fkeyConstraint.UpdateRule = Rule.Cascade
    fkeyConstraint.AcceptRejectRule = AcceptRejectRule.Cascade

    ' Add the constraint, and set EnforceConstraints to true.
    suppliersProducts.Tables("Products").Constraints.Add(fkeyConstraint)
    suppliersProducts.EnforceConstraints = True
End Sub


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

This type is safe for multithreaded read operations. You must synchronize any write operations.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ