AddRelation Method
The AddRelation method registers a parent / child relationship in IDataSet.Relations.
In the POCO DataSet core model, relations are stored as metadata (they do not automatically enforce constraints).
Once a relation is registered, you can use navigation helpers like GetChildRows and TryGetParentRow,
or validate referential integrity using ValidateRelations / EnsureRelationsValid.
/// <summary>
/// Adds relation between parent and child tables in a data set
/// IDataSet interface implementation
/// </summary>
/// <param name="relationName">Relation name</param>
/// <param name="parentTableName">Parent table name</param>
/// <param name="parentColumnName">Parent column name</param>
/// <param name="childTableName">Child table name</param>
/// <param name="childColumnName">Child column name</param>
IDataRelation AddRelation(string relationName, string parentTableName, string parentColumnName, string childTableName, string childColumnName)
/// <summary>
/// Adds relation between parent and child tables in a data set using composite keys
/// IDataSet interface implementation
/// </summary>
/// <param name="relationName">Relation name</param>
/// <param name="parentTableName">Parent table name</param>
/// <param name="parentColumnNames">Parent column names</param>
/// <param name="childTableName">Child table name</param>
/// <param name="childColumnNames">Child column names</param>
public static void AddRelation(string relationName, string parentTableName, IList<string> parentColumnNames, string childTableName, IList<string> childColumnNames)
Namespace: PocoDataSet.Data
Assembly: PocoDataSet.Data.dll
dataSet is null, the method returns without doing anything.dataSet.Relations is null, it is initialized to an empty list.IDataSet.Relations.Simple primary and foreign keys example
// 1. Create new data set
IDataSet dataSet = DataSetFactory.CreateDataSet();
// 2. Add parent Customer table with Id primary key
IDataTable customerDataTable = dataSet.AddNewTable("Customer");
customerDataTable.AddColumn("Id", DataTypeNames.INT32);
customerDataTable.AddColumn("Name", DataTypeNames.STRING);
// 3. Add child Order table with CustomerId foreign key
IDataTable orderDataTable = dataSet.AddNewTable("Order");
orderDataTable.AddColumn("Id", DataTypeNames.INT32);
orderDataTable.AddColumn("CustomerId", DataTypeNames.INT32, false, false, true);
orderDataTable.AddColumn("Status", DataTypeNames.STRING);
// 4. Add relation between Customer and Order tables: Customer.Id -> Order.CustomerId
dataSet.AddRelation("CustomerOrder", "Customer", "Id", "Order", "CustomerId");
// Insert data
IDataRow customerDataRow = customerDataTable.AddNewRow();
customerDataRow["Id"] = 1;
customerDataRow["Name"] = "Acme";
customerDataTable.AcceptChanges();
IDataRow orderDataRow = orderDataTable.AddNewRow();
orderDataRow["Id"] = 1;
orderDataRow["CustomerId"] = 1;
orderDataRow["Status"] = "Open";
orderDataTable.AcceptChanges();
// Now relation navigation works (see GetChildRows / TryGetParentRow docs).
bool hasRelation = dataSet.ContainsRelation("CustomerOrder"); // true
Composite primary and foreign keys example
// 1. Create new data set
IDataSet dataSet = DataSetFactory.CreateDataSet();
// 2. Add parent Customer table with Code1 and Code2 primary keys
IDataTable customerDataTable = dataSet.AddNewTable("Customer");
customerDataTable.AddColumn("Code1", DataTypeNames.STRING, false, true);
customerDataTable.AddColumn("Code2", DataTypeNames.STRING, false, true);
customerDataTable.AddColumn("Name", DataTypeNames.STRING);
// 3. Add child Order table with CustomerCode1 and CustomerCode2 composite foreign key
IDataTable orderDataTable = dataSet.AddNewTable("Order");
orderDataTable.AddColumn("Id", DataTypeNames.INT32, false, true);
orderDataTable.AddColumn("CustomerCode1", DataTypeNames.STRING, false, false, true);
orderDataTable.AddColumn("CustomerCode2", DataTypeNames.STRING, false, false, true);
orderDataTable.AddColumn("Status", DataTypeNames.STRING);
// Add relation between Customer and Order tables
// Customer.Code1 -> Order.CustomerCode1
// Customer.Code2 -> Order.CustomerCode2
dataSet.AddRelation("CustomerOrder", "Customer", new List<string> { "Code1", "Code2" }, "Order", new List<string> { "CustomerCode1", "CustomerCode2" });
// Insert data
IDataRow customerDataRow = customerDataTable.AddNewRow();
customerDataRow["Code1"] = "Acme";
customerDataRow["Code2"] = "Group";
customerDataRow["Name"] = "Performance";
customerDataTable.AcceptChanges();
IDataRow orderDataRow = orderDataTable.AddNewRow();
orderDataRow["OrderId"] = 1;
orderDataRow["CustomerCode1"] = "Acme";
orderDataRow["CustomerCode2"] = "Group";
orderDataRow["Status"] = "Open";
orderDataTable.AcceptChanges();
// Now relation navigation works (see GetChildRows / TryGetParentRow docs).
bool hasRelation = dataSet.ContainsRelation("CustomerOrder"); // true
Table of Content POCO DataSet API References POCO DataSet Types 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.