Muokkaa

Jaa


Turn off constraints while filling a dataset in .NET Framework applications

Note

Datasets and related classes are legacy .NET Framework technologies from the early 2000s that enable applications to work with data in memory while the applications are disconnected from the database. The technologies are especially useful for applications that enable users to modify data and persist the changes back to the database. Although datasets have proven to be a very successful technology, we recommend that new .NET applications use Entity Framework Core. Entity Framework provides a more natural way to work with tabular data as object models, and it has a simpler programming interface.

If a dataset contains constraints (such as foreign-key constraints), they can raise errors related to the order of operations that are performed against the dataset. For example, loading child records before loading related parent records can violate a constraint and cause an error. As soon as you load a child record, the constraint checks for the related parent record and raises an error.

If there were no mechanism to allow temporary constraint suspension, an error would be raised every time you tried to load a record into the child table. Another way to suspend all constraints in a dataset is with the BeginEdit, and EndEdit properties.

Note

Validation events (for example, ColumnChanging and RowChanging) will not be raised when constraints are turned off.

To suspend update constraints programmatically

  • The following example shows how to temporarily turn off constraint checking in a dataset:

    dataSet1.EnforceConstraints = false;
    // Perform some operations on the dataset
    dataSet1.EnforceConstraints = true;
    

To suspend update constraints using the Dataset Designer

  1. Open your dataset in the Dataset Designer. For more information, see Walkthrough: Creating a dataset in the Dataset Designer.

  2. In the Properties window, set the EnforceConstraints property to false.