ValidateRelations Method
The ValidateRelations extension method validates referential integrity using the relation metadata stored in
IDataSet.Relations.
In POCO DataSet, relations are stored as metadata (table/column pairs). Navigation and integrity checks are performed by extensions
(for example, GetChildRows, TryGetParentRow, and EnsureRelationsValid).
ValidateRelations is the non-throwing API: it returns a list of violations you can inspect and decide how to handle.
/// <summary>
/// Validates referential integrity based on <see cref="IDataSet.Relations"/>.
/// The current POCO DataSet core model stores relations as metadata only; this extension performs the
/// integrity checks when you need them (e.g., before SaveChanges, before AcceptChanges, etc.).
/// </summary>
/// <param name="dataSet">Data set</param>
/// <param name="options">Validation options (optional)</param>
/// <returns>List of violations (empty if none)</returns>
public static List<RelationIntegrityViolation> ValidateRelations(this IDataSet? dataSet, RelationValidationOptions? options = null)
Namespace: PocoDataSet.Extensions
Assembly: PocoDataSet.Extensions
dataSet: The data set whose relations should be validated. If null, an empty list is returned.
options: Optional validation options. When not provided, defaults are used via new RelationValidationOptions().
Returns a List<RelationIntegrityViolation>.
If validation finds no issues, the list is empty.
The example below shows a typical workflow:
// Build a DataSet with two tables:
// Customers (parent) and Orders (child)
IDataSet dataSet = new DataSet();
IDataTable customers = new DataTable("Customers");
customers.AddColumn("Id", typeof(int), isPrimaryKey: true);
customers.AddColumn("Name", typeof(string));
dataSet.AddTable(customers);
IDataTable orders = new DataTable("Orders");
orders.AddColumn("OrderId", typeof(int), isPrimaryKey: true);
orders.AddColumn("CustomerId", typeof(int));
orders.AddColumn("Status", typeof(string));
dataSet.AddTable(orders);
// Relation metadata: Customers.Id -> Orders.CustomerId
dataSet.AddRelation(
"CustomerOrders",
"Customers",
new List<string> { "Id" },
"Orders",
new List<string> { "CustomerId" });
// Parent row
IDataRow c1 = DataRowExtensions.CreateRowFromColumns(customers.Columns);
c1["Id"] = 1;
c1["Name"] = "Acme";
customers.AddLoadedRow(c1);
// Child row (valid)
IDataRow o10 = DataRowExtensions.CreateRowFromColumns(orders.Columns);
o10["OrderId"] = 10;
o10["CustomerId"] = 1;
o10["Status"] = "Open";
orders.AddLoadedRow(o10);
// Validate: no violations
List<RelationIntegrityViolation> ok = dataSet.ValidateRelations();
// ok.Count == 0
// Child row (orphan: CustomerId=999 does not exist in Customers.Id)
IDataRow o11 = DataRowExtensions.CreateRowFromColumns(orders.Columns);
o11["OrderId"] = 11;
o11["CustomerId"] = 999;
o11["Status"] = "Open";
orders.AddLoadedRow(o11);
List<RelationIntegrityViolation> violations = dataSet.ValidateRelations();
// violations.Count == 1
// violations[0].Kind == RelationIntegrityViolationKind.OrphanChildRow
// For sparse changesets you may disable orphan checks:
RelationValidationOptions options = new RelationValidationOptions();
options.EnforceOrphanChecks = false;
List<RelationIntegrityViolation> definitionOnly = dataSet.ValidateRelations(options);
// definitionOnly will only include "invalid definition" / "delete restrict" problems (depending on options)
ValidateRelations when you want to display a friendly error list to the user or log diagnostics without throwing.
EnsureRelationsValid when you want an exception to fail fast if any violation exists.
RelationValidationOptions.EnforceOrphanChecks to false.
Table of Content POCO DataSet DataSet Members
Business Process Programming in .Net
© 2004–2026 Laskarzhevsky Software Inc.
Unless otherwise noted, the content of this website is licensed under the
Creative Commons Attribution 4.0 International License (CC BY 4.0).
Code examples are provided under the MIT License.
You are free to share and adapt the material provided that appropriate
credit is given and any modifications are clearly indicated.
The information provided on this website is for educational purposes only.
The author and publisher make no warranties regarding the completeness
or suitability of the information and are not responsible for any damages
resulting from its use.