In an ADO.NET DataSet named customerOrders, you enforce constraints and define a ForeignKeyConstraint ordersFK between the Customers and Orders tables. You want an exception to be thrown if code attempts to delete a Customer row that still has related Order rows. Which delete rule setting should you apply to the foreign key constraint?

Difficulty: Medium

Correct Answer: ordersFK.DeleteRule = Rule.None;

Explanation:


Introduction / Context:
When modeling relational data in an ADO.NET DataSet, you can define ForeignKeyConstraint objects to enforce relationships between parent and child tables. The DeleteRule property of a ForeignKeyConstraint determines what happens to child rows when a parent row is deleted. Choosing the correct rule is critical to match business rules and to ensure that invalid deletions raise exceptions instead of silently modifying or removing related data.


Given Data / Assumptions:

    The DataSet is named customerOrders and EnforceConstraints is set to true.

    Customers is the parent table, and Orders is the child table, linked with a ForeignKeyConstraint called ordersFK based on the CustomerID column.

    You want to prevent deletion of a Customer record if any related Order records exist in the Orders table.

    When an attempt is made to delete such a Customer row, an exception should be thrown rather than cascading or setting foreign keys to null.

    The code snippet shows that the constraint is added to the Orders table Constraints collection.


Concept / Approach:
The DeleteRule property supports several values: Rule.Cascade, Rule.SetNull, Rule.SetDefault, and Rule.None. Rule.Cascade automatically deletes child rows when the parent is deleted. Rule.SetNull and Rule.SetDefault update the child foreign key values rather than blocking deletion. Rule.None enforces strict referential integrity by disallowing deletion of a parent row that has related children, which causes an exception when such a deletion is attempted. Since the goal is to cause an exception and block deletion, Rule.None is the correct choice.


Step-by-Step Solution:
1. Recognize that EnforceConstraints is true, so the DataSet will actively enforce relational rules defined by constraints. 2. Identify that the business rule requires blocking deletion of a Customer that has any Orders, which means you cannot use a cascading or null setting. 3. Evaluate Rule.Cascade: this would automatically delete all related Orders, which is not desired. 4. Evaluate Rule.SetNull and Rule.SetDefault: these would preserve child rows but break or modify their relationship to the Customer, which also does not meet the requirement to raise an exception. 5. Choose Rule.None, which enforces strict referential integrity and causes an exception when a parent delete violates the foreign key constraint.


Verification / Alternative check:
You can verify the behavior by writing a small test that adds a Customer with related Orders to the DataSet, then attempts to delete the Customer row with Rule.None in place. When you call AcceptChanges or otherwise commit the deletion, an exception should be thrown, confirming that Rule.None prevents the operation. Changing DeleteRule to Cascade will remove the exception and delete child rows, demonstrating the difference.


Why Other Options Are Wrong:
Rule.SetNull would set the CustomerID field in Orders to null, allowing deletion of the Customer but not preserving strict referential integrity.
Rule.SetDefault would change child foreign keys to a default value and still allow deletion, which does not raise the required exception.
Rule.Cascade would automatically delete all Orders for that Customer, which contradicts the requirement to block deletion and throw an exception.


Common Pitfalls:
A frequent pitfall is using Cascade by default because it appears convenient, without considering the business requirement that child records must remain as an audit trail. Another mistake is overlooking how SetNull or SetDefault can lead to orphaned child rows if not carefully managed. Selecting Rule.None clearly communicates that parent deletions are not allowed when related children exist, enforcing a strong data integrity rule.


Final Answer:
You should set ordersFK.DeleteRule = Rule.None; so that deleting a Customer with related Orders throws an exception.

More Questions from Microsoft Certification

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion