public DataSet ToUntypedDataSet() { var dataSet = new DataSet(); dataSet.DataSetName = DataSetName; dataSet.CaseSensitive = CaseSensitive; dataSet.EnforceConstraints = EnforceConstraints; dataSet.Locale = Locale; dataSet.Namespace = Namespace; dataSet.Prefix = Prefix; dataSet.RemotingFormat = RemotingFormat; foreach (var typedTable in Tables.Cast <DataTable>()) { var dataTable = new DataTable(); dataTable.TableName = typedTable.TableName; dataTable.DisplayExpression = typedTable.DisplayExpression; dataTable.MinimumCapacity = typedTable.MinimumCapacity; dataTable.Namespace = typedTable.Namespace; dataTable.Prefix = typedTable.Prefix; dataTable.RemotingFormat = typedTable.RemotingFormat; foreach (var column in typedTable.Columns.Cast <DataColumn>()) { var dataColumn = new DataColumn(); dataColumn.ColumnName = column.ColumnName; dataColumn.AllowDBNull = column.AllowDBNull; dataColumn.AutoIncrement = column.AutoIncrement; dataColumn.AutoIncrementSeed = column.AutoIncrementSeed; dataColumn.AutoIncrementStep = column.AutoIncrementStep; dataColumn.Caption = column.Caption; dataColumn.ColumnMapping = column.ColumnMapping; dataColumn.DataType = column.DataType; dataColumn.DateTimeMode = column.DateTimeMode; dataColumn.DefaultValue = column.DefaultValue; dataColumn.Expression = column.Expression; dataColumn.MaxLength = column.MaxLength; dataColumn.Namespace = column.Namespace; dataColumn.Prefix = column.Prefix; dataColumn.ReadOnly = column.ReadOnly; foreach (var property in column.ExtendedProperties.Cast <DictionaryEntry>()) { dataColumn.ExtendedProperties.Add(property.Key, property.Value); } dataTable.Columns.Add(dataColumn); } foreach (var row in typedTable.Rows.Cast <DataRow>()) { dataTable.ImportRow(row); } dataTable.PrimaryKey = typedTable.PrimaryKey .Select(col => dataTable.Columns[col.ColumnName]) .ToArray(); foreach (var property in typedTable.ExtendedProperties.Cast <DictionaryEntry>()) { dataTable.ExtendedProperties.Add(property.Key, property.Value); } foreach (var constraint in typedTable.Constraints.Cast <Constraint>()) { if (!Relations.Cast <DataRelation>().Any(rel => (rel.ChildKeyConstraint == constraint) || (rel.ParentKeyConstraint == constraint))) { if (constraint is UniqueConstraint uniqueConstraint) { if (!uniqueConstraint.IsPrimaryKey) { dataTable.Constraints.Add(new UniqueConstraint( name: uniqueConstraint.ConstraintName, column: dataTable.Columns[uniqueConstraint.Columns.Single().ColumnName])); } } else { throw new Exception($"Don't know how to clone a constraint of type {constraint.GetType()}"); } } } dataSet.Tables.Add(dataTable); } foreach (var property in ExtendedProperties.Cast <DictionaryEntry>()) { dataSet.ExtendedProperties.Add(property.Key, property.Value); } foreach (var relation in Relations.Cast <DataRelation>()) { // NB: In the context of the unit test, this is assuming that there is only one column in a relation. var dataRelation = new DataRelation( relation.RelationName, parentColumn: dataSet.Tables[relation.ParentTable.TableName].Columns[relation.ParentColumns.Single().ColumnName], childColumn: dataSet.Tables[relation.ChildTable.TableName].Columns[relation.ChildColumns.Single().ColumnName]); foreach (var property in relation.ExtendedProperties.Cast <DictionaryEntry>()) { dataRelation.ExtendedProperties.Add(property.Key, property.Value); } dataSet.Relations.Add(dataRelation); } return(dataSet); }