First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know why the DataRelation
in the DataSet was not cloned as subclass types.
Yes. That is the problem.
Could you let me know how you created the subclasses? Are they generated
by
VS.NET for a typed DataSet?
It is NOT a VS typed DataSet. I have simply created my own data relation
class that inherits from System.Data.DataRelation.
The documentation for DataSet.Clone states that it:
"Copies the structure of the DataSet, including all DataTable schemas,
relations, and constraints. Does not copy any data."
And in the Remarks section it states:
"Note If these classes have been subclassed, the clone will also be of the
same subclasses."
The documentation is not consistent with the behavior. The following sample
illustrates the problem:
using System.Diagnostics;
using System.Data;
namespace DataRelationCloneBug
{
public class MyDataSet : System.Data.DataSet
{
public MyDataSet()
{
}
}
public class MyDataTable : System.Data.DataTable
{
public MyDataTable()
{
}
}
public class MyDataRelation : System.Data.DataRelation
{
public MyDataRelation( string relationName, DataColumn parentColumn,
DataColumn childColumn)
: base( relationName, parentColumn, childColumn )
{
}
}
public class Program
{
[System.STAThread]
static void Main()
{
MyDataSet DataSet1 = new MyDataSet();
MyDataTable Table1 = new MyDataTable();
Table1.TableName = "Table1";
Table1.Columns.Add("PK");
DataSet1.Tables.Add( Table1 );
MyDataTable Table2 = new MyDataTable();
Table2.TableName = "Table2";
Table2.Columns.Add( "FK" );
DataSet1.Tables.Add( Table2 );
DataSet1.Relations.Add( new MyDataRelation( "Relation1",
Table1.Columns["PK"], Table2.Columns["FK"]) );
MyDataSet DataSet2 = DataSet1.Clone() as MyDataSet;
Trace.Write( "The cloned data set is of type " );
Trace.Write( DataSet2.GetType().Name );
Trace.Write( " and Table1 is of type " );
Trace.Write( DataSet2.Tables["Table1"].GetType().Name );
Trace.Write( ", but Relation1 is of type " );
Trace.Write( DataSet2.Relations["Relation1"].GetType().Name );
Trace.WriteLine( "." );
}
}
}