EnsureRelationsValid Method
The EnsureRelationsValid method validates referential integrity based on the relations registered in IDataSet.Relations.
If at least one violation is found (for example, a child row references a missing parent row), the method throws an InvalidOperationException with a readable diagnostic message.
/// <summary>
/// Validates relations and throws an exception if any violation exists.
/// </summary>
/// <param name="dataSet">Data set</param>
/// <param name="options">Validation options (optional)</param>
/// <exception cref="InvalidOperationException">Thrown when at least one violation exists</exception>
public static void EnsureRelationsValid(this IDataSet? dataSet, RelationValidationOptions? options = null)
Namespace: PocoDataSet.Extensions
Assembly: PocoDataSet.Extensions.dll
dataSet is null, the method returns without throwing.ValidateRelations and throws if the list is not empty.
// Scenario: An Employee references a Department that does not exist.
IDataSet dataSet = DataSetFactory.CreateDataSet();
IDataTable dept = dataSet.AddNewTable("Department");
dept.AddColumn("Id", DataTypeNames.INT32, isNullable: false, isPrimaryKey: true);
dept.AddColumn("Name", DataTypeNames.STRING);
IDataTable emp = dataSet.AddNewTable("Employee");
emp.AddColumn("Id", DataTypeNames.INT32, isNullable: false, isPrimaryKey: true);
emp.AddColumn("DepartmentId", DataTypeNames.INT32, isNullable: false, isPrimaryKey: false);
emp.AddColumn("FirstName", DataTypeNames.STRING);
dataSet.AddRelation(
"DepartmentEmployees",
"Department",
new List<string> { "Id" },
"Employee",
new List<string> { "DepartmentId" });
// Add an employee that references DepartmentId=999, but no department row exists.
IDataRow e1 = emp.AddNewRow();
e1["Id"] = 1;
e1["DepartmentId"] = 999;
e1["FirstName"] = "Sara";
emp.AcceptChanges();
// Throws InvalidOperationException with details about the violation.
dataSet.EnsureRelationsValid();
ValidateRelations if you prefer a non-throwing API that returns a list of violations.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.