context: // Authors:
  //   Nagappan A <anagappan@novell.com>
  //
  // Copyright (c) 2007 Novell, Inc
  //
  // Permission is hereby granted, free of charge, to any person obtaining
  // a copy of this software and associated documentation files (the
  // "Software"), to deal in the Software without restriction, including
  // without limitation the rights to use, copy, modify, merge, publish,
  // distribute, sublicense, and/or sell copies of the Software, and to
  // permit persons to whom the Software is furnished to do so, subject to
  // the following conditions:
  //
  // The above copyright notice and this permission notice shall be
  // included in all copies or substantial portions of the Software.
  //
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  //
  
  using System;
  using System.Data;
  using System.IO;
  using System.Xml;
  
  using NUnit.Framework;
  
  namespace MonoTests.System.Data
  {
  	[TestFixture]
  	public class DataTableTest3
  	{
  		string tempFile;
  		DataSet dataSet;
  		DataTable parentTable;
  		DataTable childTable;
  		DataTable secondChildTable;
  
  		[SetUp]
  		public void SetUp ()
  		{
  			tempFile = Path.GetTempFileName ();
  		}
  
  		[TearDown]
  		public void TearDown ()
  		{
  			if (tempFile != null)
  				File.Delete (tempFile);
  		}
  
  		private void MakeParentTable ()
  		{
  			// Create a new Table
  			parentTable = new DataTable ("ParentTable");
  			dataSet = new DataSet ("XmlSchemaDataSet");
  			DataColumn column;
  			DataRow row;
   
  			// Create new DataColumn, set DataType,
  			// ColumnName and add to Table.
  			column = new DataColumn ();
  			column.DataType = typeof (int);
  			column.ColumnName = "id";
  			column.Unique = true;
  			// Add the Column to the DataColumnCollection.
  			parentTable.Columns.Add (column);
  
  			// Create second column
  			column = new DataColumn ();
  			column.DataType = typeof (string);
  			column.ColumnName = "ParentItem";
  			column.AutoIncrement = false;
  			column.Caption = "ParentItem";
  			column.Unique = false;
  			// Add the column to the table
  			parentTable.Columns.Add (column);
  
  			// Create third column.
  			column = new DataColumn ();
  			column.DataType = typeof (int);
  			column.ColumnName = "DepartmentID";
  			column.Caption = "DepartmentID";
  			// Add the column to the table.
  			parentTable.Columns.Add (column);
  
  			// Make the ID column the primary key column.
  			DataColumn [] PrimaryKeyColumns = new DataColumn [2];
  			PrimaryKeyColumns [0] = parentTable.Columns ["id"];
  			PrimaryKeyColumns [1] = parentTable.Columns ["DepartmentID"];
  			parentTable.PrimaryKey = PrimaryKeyColumns;
  
  			dataSet.Tables.Add (parentTable);
   
  			// Create three new DataRow objects and add 
  			// them to the DataTable
  			for (int i = 0; i <= 2; i++) {
  				row = parentTable.NewRow ();
  				row ["id"] = i + 1 ;
  				row ["ParentItem"] = "ParentItem " + (i + 1);
  				row ["DepartmentID"] = i + 1;
  				parentTable.Rows.Add (row);
  			}
  		}
  
  		private void MakeChildTable ()
  		{
  			// Create a new Table
  			childTable = new DataTable ("ChildTable");
  			DataColumn column;
  			DataRow row;
   
  			// Create first column and add to the DataTable.
  			column = new DataColumn ();
  			column.DataType= typeof (int);
  			column.ColumnName = "ChildID";
  			column.AutoIncrement = true;
  			column.Caption = "ID";
  			column.Unique = true;
  
  			// Add the column to the DataColumnCollection
  			childTable.Columns.Add (column);
  
  			// Create second column
  			column = new DataColumn ();
  			column.DataType= typeof (string);
  			column.ColumnName = "ChildItem";
  			column.AutoIncrement = false;
  			column.Caption = "ChildItem";
  			column.Unique = false;
  			childTable.Columns.Add (column);
  			 
  			//Create third column
  			column = new DataColumn ();
  			column.DataType= typeof (int);
  			column.ColumnName = "ParentID";
  			column.AutoIncrement = false;
  			column.Caption = "ParentID";
  			column.Unique = false;
  			childTable.Columns.Add (column);
  
  			dataSet.Tables.Add (childTable);
   
  			// Create three sets of DataRow objects,
  			// five rows each, and add to DataTable.
  			for (int i = 0; i <= 1; i ++) {
  				row = childTable.NewRow ();
  				row ["childID"] = i + 1;
  				row ["ChildItem"] = "ChildItem " + (i + 1);
  				row ["ParentID"] = 1 ;
  				childTable.Rows.Add (row);
  			}
  
  			for (int i = 0; i <= 1; i ++) {
  				row = childTable.NewRow ();
  				row ["childID"] = i + 5;
  				row ["ChildItem"] = "ChildItem " + (i + 1);
  				row ["ParentID"] = 2 ;
  				childTable.Rows.Add (row);
  			}
  
  			for (int i = 0; i <= 1; i ++) {
  				row = childTable.NewRow ();
  				row ["childID"] = i + 10;
  				row ["ChildItem"] = "ChildItem " + (i + 1);
  				row ["ParentID"] = 3 ;
  				childTable.Rows.Add (row);
  			}
  		}
  
  		private void MakeSecondChildTable ()
  		{
  			// Create a new Table
  			secondChildTable = new DataTable ("SecondChildTable");
  			DataColumn column;
  			DataRow row;
   
  			// Create first column and add to the DataTable.
  			column = new DataColumn ();
  			column.DataType= typeof (int);
  			column.ColumnName = "ChildID";
  			column.AutoIncrement = true;
  			column.Caption = "ID";
  			column.ReadOnly = true;
  			column.Unique = true;
  
  			// Add the column to the DataColumnCollection.
  			secondChildTable.Columns.Add (column);
  
  			// Create second column.
  			column = new DataColumn ();
  			column.DataType= typeof (string);
  			column.ColumnName = "ChildItem";
  			column.AutoIncrement = false;
  			column.Caption = "ChildItem";
  			column.ReadOnly = false;
  			column.Unique = false;
  			secondChildTable.Columns.Add (column);
  
  			//Create third column.
  			column = new DataColumn ();
  			column.DataType= typeof (int);
  			column.ColumnName = "ParentID";
  			column.AutoIncrement = false;
  			column.Caption = "ParentID";
  			column.ReadOnly = false;
  			column.Unique = false;
  			secondChildTable.Columns.Add (column);
  
  			//Create fourth column.
  			column = new DataColumn ();
  			column.DataType= typeof (int);
  			column.ColumnName = "DepartmentID";
  			column.Caption = "DepartmentID";
  			column.Unique = false;
  			secondChildTable.Columns.Add (column);
  
  			dataSet.Tables.Add (secondChildTable);
  
  			// Create three sets of DataRow objects,
  			// five rows each, and add to DataTable.
  			for (int i = 0; i <= 1; i++) {
  				row = secondChildTable.NewRow ();
  				row ["childID"] = i + 1;
  				row ["ChildItem"] = "SecondChildItem " + (i + 1);
  				row ["ParentID"] = 1 ;
  				row ["DepartmentID"] = 1;
  				secondChildTable.Rows.Add (row);
  			}
  
  			for (int i = 0;i <= 1;i++) {
  				row = secondChildTable.NewRow ();
  				row ["childID"] = i + 5;
  				row ["ChildItem"] = "SecondChildItem " + (i + 1);
  				row ["ParentID"] = 2;
  				row ["DepartmentID"] = 2;
  				secondChildTable.Rows.Add (row);
  			}
  
  			for (int i = 0;i <= 1;i++) {
  				row = secondChildTable.NewRow ();
  				row ["childID"] = i + 10;
  				row ["ChildItem"] = "SecondChildItem " + (i + 1);
  				row ["ParentID"] = 3 ;
  				row ["DepartmentID"] = 3;
  				secondChildTable.Rows.Add (row);
  			}
  		}
  
  		private void MakeDataRelation ()
  		{
  			DataColumn parentColumn = dataSet.Tables ["ParentTable"].Columns ["id"];
  			DataColumn childColumn = dataSet.Tables ["ChildTable"].Columns ["ParentID"];
  			DataRelation relation = new DataRelation ("ParentChild_Relation1", parentColumn, childColumn);
  
gt: 			dataSet.Tables ["ChildTable"].ParentRelations.Add (relation);