AddRelation Method

Overview

The AddRelation method registers a parent / child relationship in IObservableDataSet.Relations, which are used for:.

This method delegates to the underlying POCO IDataSet.AddRelation.

Declaration

/// <summary>
/// Adds relation between parent and child tables in a data set
/// IObservableDataSet 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
/// IObservableDataSet 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 and Assembly

Namespace: PocoDataSet.ObservableData
Assembly: PocoDataSet.ObservableData.dll

Usage Example

Simple primary and foreign keys example

// 1. Create observable dataset
IObservableDataSet observableDataSet = new ObservableDataSet();

// 2. Add parent Customer table with Id primary key
IObservableDataTable customerObservableDataTable = observableDataSet.AddNewTable("Customer");
customerObservableDataTable.AddColumn("Id", DataTypeNames.INT32);
customerObservableDataTable.AddColumn("Name", DataTypeNames.STRING);

// 3. Add child Order table with CustomerId foreign key
IObservableDataTable orderObservableDataTable = observableDataSet.AddNewTable("Order");
orderObservableDataTable.AddColumn("Id", DataTypeNames.INT32);
orderObservableDataTable.AddColumn("CustomerId", DataTypeNames.INT32, false, false, true);
orderObservableDataTable.AddColumn("Status", DataTypeNames.STRING);

// 4. Add relation between Customer and Order tables: Customer.Id -> Order.CustomerId
observableDataSet.AddRelation("CustomerOrder", "Customer", "Id", "Order", "CustomerId");

// Insert data
IObservableDataRow customerObservableDataRow = customerObservableDataTable.AddNewRow();
customerObservableDataRow["Id"] = 1;
customerObservableDataRow["Name"] = "Acme";
customerObservableDataTable.AcceptChanges();

IObservableDataRow orderObservableDataRow = orderObservableDataTable.AddNewRow();
orderObservableDataRow["Id"] = 1;
orderObservableDataRow["CustomerId"] = 1;
orderObservableDataRow["Status"] = "Open";
orderObservableDataTable.AcceptChanges();

// Now relation navigation works (see GetChildRows / TryGetParentRow docs).
bool hasRelation = observableDataSet.ContainsRelation("CustomerOrder"); // true

Composite primary and foreign keys example

// 1. Create observable dataset
IObservableDataSet observableDataSet = new ObservableDataSet();

// 2. Add parent Customer table with Code1 and Code2 primary keys
IObservableDataTable customerObservableDataTable = observableDataSet.AddNewTable("Customer");
customerObservableDataTable.AddColumn("Code1", DataTypeNames.STRING, false, true);
customerObservableDataTable.AddColumn("Code2", DataTypeNames.STRING, false, true);
customerObservableDataTable.AddColumn("Name", DataTypeNames.STRING);

// 3. Add child Order table with CustomerCode1 and CustomerCode2 composite foreign key
IObservableDataTable orderObservableDataTable = observableDataSet.AddNewTable("Order");
orderObservableDataTable.AddColumn("Id", DataTypeNames.INT32, false, true);
orderObservableDataTable.AddColumn("CustomerCode1", DataTypeNames.STRING, false, false, true);
orderObservableDataTable.AddColumn("CustomerCode2", DataTypeNames.STRING, false, false, true);
orderObservableDataTable.AddColumn("Status", DataTypeNames.STRING);

// Add relation between Customer and Order tables
// Customer.Code1 -> Order.CustomerCode1
// Customer.Code2 -> Order.CustomerCode2
observableDataSet.AddRelation("CustomerOrder", "Customer", new List<string> { "Code1", "Code2" }, "Order", new List<string> { "CustomerCode1", "CustomerCode2" });

// Insert data
IObservableDataRow customerObservableDataRow = customerObservableDataTable.AddNewRow();
customerObservableDataRow["Code1"] = "Acme";
customerObservableDataRow["Code2"] = "Group";
customerObservableDataRow["Name"] = "Performance";
customerObservableDataTable.AcceptChanges();

IObservableDataRow orderObservableDataRow = orderObservableDataTable.AddNewRow();
orderObservableDataRow["OrderId"] = 1;
orderObservableDataRow["CustomerCode1"] = "Acme";
orderObservableDataRow["CustomerCode2"] = "Group";
orderObservableDataRow["Status"] = "Open";
orderObservableDataTable.AcceptChanges();

// Now relation navigation works (see GetChildRows / TryGetParentRow docs).
bool hasRelation = observableDataSet.ContainsRelation("CustomerOrder"); // true

 

Table of Content POCO DataSet API References ObservablePOCO DataSet Types ObservableDataSet 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.