protected override void RemoveCore(DataRelation relation) { base.RemoveCore(relation); _dataSet.OnRemoveRelationHack(relation); relation.SetDataSet(null); relation.ChildKey.GetSortIndex().RemoveRef(); if (relation.Nested) { relation.ChildTable.CacheNestedParent(); } for (int i = 0; i < _relations.Count; i++) { if (relation == _relations[i]) { _relations.RemoveAt(i); ((DataTableRelationCollection)(relation.ParentTable.ChildRelations)).Remove(relation); // Remove Cache from ParentTable -> ChildRelations ((DataTableRelationCollection)(relation.ChildTable.ParentRelations)).Remove(relation); // Removing Cache from ChildTable -> ParentRelations if (relation.Nested) { relation.ChildTable.CacheNestedParent(); } UnregisterName(relation.RelationName); relation.SetParentKeyConstraint(null); relation.SetChildKeyConstraint(null); return; } } throw ExceptionBuilder.RelationDoesNotExist(); }
protected override void RemoveCore(DataRelation relation) { base.RemoveCore(relation); relation.SetDataSet(null); relation.ParentTable.ChildRelations.Remove(relation); relation.ChildTable.ParentRelations.Remove(relation); relation.SetParentKeyConstraint(null); relation.SetChildKeyConstraint(null); }
/// <include file='doc\ConstraintCollection.uex' path='docs/doc[@for="ConstraintCollection.Remove"]/*' /> /// <devdoc> /// <para> /// Removes the specified <see cref='System.Data.Constraint'/> /// from the collection.</para> /// </devdoc> public void Remove(Constraint constraint) { if (constraint == null) { throw ExceptionBuilder.ArgumentNull("constraint"); } // this will throw an exception if it can't be removed, otherwise indicates // whether we need to remove it from the collection. if (CanRemove(constraint, true)) { // constraint can be removed BaseRemove(constraint); ArrayRemove(constraint); if (constraint is UniqueConstraint && ((UniqueConstraint)constraint).IsPrimaryKey) { Table.PrimaryKey = null; } if (constraint is UniqueConstraint) { for (int i = 0; i < Table.ChildRelations.Count; i++) { DataRelation rel = Table.ChildRelations[i]; if (rel.ParentKeyConstraint == constraint) { rel.SetParentKeyConstraint(null); } } DataKey key = ((UniqueConstraint)constraint).Key; key.GetSortIndex().RemoveRef(); } else if (constraint is ForeignKeyConstraint) { for (int i = 0; i < Table.ParentRelations.Count; i++) { DataRelation rel = Table.ParentRelations[i]; if (rel.ChildKeyConstraint == constraint) { rel.SetChildKeyConstraint(null); } } } OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, constraint)); } }
/// <summary> /// Does verification on the constraint and it's name. /// An ArgumentNullException is thrown if this constraint is null. An ArgumentException is thrown /// if this constraint doesn't belong to this collection or if this constraint is part of a relationship. /// </summary> private void BaseRemove(Constraint constraint) { if (constraint == null) { throw ExceptionBuilder.ArgumentNull(nameof(constraint)); } if (constraint.Table != _table) { throw ExceptionBuilder.ConstraintRemoveFailed(); } UnregisterName(constraint.ConstraintName); constraint.InCollection = false; if (constraint is UniqueConstraint) { for (int i = 0; i < Table.ChildRelations.Count; i++) { DataRelation rel = Table.ChildRelations[i]; if (rel.ParentKeyConstraint == constraint) { rel.SetParentKeyConstraint(null); } } ((UniqueConstraint)constraint).ConstraintIndexClear(); } else if (constraint is ForeignKeyConstraint) { for (int i = 0; i < Table.ParentRelations.Count; i++) { DataRelation rel = Table.ParentRelations[i]; if (rel.ChildKeyConstraint == constraint) { rel.SetChildKeyConstraint(null); } } } }
protected override void RemoveCore(DataRelation relation) { base.RemoveCore(relation); dataSet.OnRemoveRelationHack(relation); relation.SetDataSet(null); relation.ChildKey.GetSortIndex().RemoveRef(); if (relation.Nested) { relation.ChildTable.CacheNestedParent(); } for (int i = 0; i < relations.Count; i++) { if (relation == relations[i]) { relations.RemoveAt(i); ((DataRelationCollection.DataTableRelationCollection)(relation.ParentTable.ChildRelations)).Remove(relation); // Remove Cache from ParentTable -> ChildRelations ((DataRelationCollection.DataTableRelationCollection)(relation.ChildTable.ParentRelations)).Remove(relation); // Removing Cache from ChildTable -> ParentRelations if (relation.Nested) relation.ChildTable.CacheNestedParent(); UnregisterName(relation.RelationName); relation.SetParentKeyConstraint(null); relation.SetChildKeyConstraint(null); return; } } throw ExceptionBuilder.RelationDoesNotExist(); }
protected override void AddCore(DataRelation relation) { base.AddCore(relation); if (relation.ChildTable.DataSet != dataSet || relation.ParentTable.DataSet != dataSet) throw ExceptionBuilder.ForeignRelation(); relation.CheckState(); if(relation.Nested) { relation.CheckNestedRelations(); } if (relation.relationName.Length == 0) relation.relationName = AssignName(); else RegisterName(relation.relationName); DataKey childKey = relation.ChildKey; for (int i = 0; i < relations.Count; i++) { if (childKey.ColumnsEqual(((DataRelation)relations[i]).ChildKey)) { if (relation.ParentKey.ColumnsEqual(((DataRelation)relations[i]).ParentKey)) throw ExceptionBuilder.RelationAlreadyExists(); } } relations.Add(relation); ((DataRelationCollection.DataTableRelationCollection)(relation.ParentTable.ChildRelations)).Add(relation); // Caching in ParentTable -> ChildRelations ((DataRelationCollection.DataTableRelationCollection)(relation.ChildTable.ParentRelations)).Add(relation); // Caching in ChildTable -> ParentRelations relation.SetDataSet(dataSet); relation.ChildKey.GetSortIndex().AddRef(); if (relation.Nested) { relation.ChildTable.CacheNestedParent(); } ForeignKeyConstraint foreignKey = relation.ChildTable.Constraints.FindForeignKeyConstraint(relation.ParentColumnsReference, relation.ChildColumnsReference); if (relation.createConstraints) { if (foreignKey == null) { relation.ChildTable.Constraints.Add(foreignKey = new ForeignKeyConstraint(relation.ParentColumnsReference, relation.ChildColumnsReference)); // try to name the fk constraint the same as the parent relation: try { foreignKey.ConstraintName = relation.RelationName; } catch (Exception e) { // if (!Common.ADP.IsCatchableExceptionType(e)) { throw; } ExceptionBuilder.TraceExceptionWithoutRethrow(e); // ignore the exception } } } UniqueConstraint key = relation.ParentTable.Constraints.FindKeyConstraint(relation.ParentColumnsReference); relation.SetParentKeyConstraint(key); relation.SetChildKeyConstraint(foreignKey); }
private void ProcessReferenceKey (XmlSchemaElement element, XmlSchemaKeyref keyref) { // Basic concept came from XmlSchemaMapper.cs string tableName = GetSelectorTarget (keyref.Selector.XPath); DataColumn [] cols; DataTable dt = dataset.Tables [tableName]; if (dt == null) throw new DataException (String.Format ("Invalid XPath selection inside selector. Cannot find: {0}", tableName)); cols = new DataColumn [keyref.Fields.Count]; int i = 0; foreach (XmlSchemaXPath Field in keyref.Fields) { string colName = Field.XPath; bool isAttr = colName.Length > 0 && colName [0] == '@'; int index = colName.LastIndexOf (':'); if (index > 0) colName = colName.Substring (index + 1); else if (isAttr) colName = colName.Substring (1); colName = XmlConvert.DecodeName (colName); DataColumn col = dt.Columns [colName]; if (isAttr && col.ColumnMapping != MappingType.Attribute) throw new DataException ("The XPath specified attribute field, but mapping type is not attribute."); if (!isAttr && col.ColumnMapping != MappingType.Element) throw new DataException ("The XPath specified simple element field, but mapping type is not simple element."); cols [i] = col; i++; } string name = keyref.Refer.Name; // get the unique constraint for the releation UniqueConstraint uniq = FindConstraint (name, element); // generate the FK. ForeignKeyConstraint fkc = new ForeignKeyConstraint(keyref.Name, uniq.Columns, cols); dt.Constraints.Add (fkc); // generate the relation. DataRelation rel = new DataRelation (keyref.Name, uniq.Columns, cols, false); if (keyref.UnhandledAttributes != null) { foreach (XmlAttribute attr in keyref.UnhandledAttributes) if (attr.LocalName == "IsNested" && attr.Value == "true" && attr.NamespaceURI == XmlConstants.MsdataNamespace) rel.Nested = true; } rel.SetParentKeyConstraint (uniq); rel.SetChildKeyConstraint (fkc); dataset.Relations.Add (rel); }
protected override void AddCore(DataRelation relation) { base.AddCore(relation); if (relation.ChildTable.DataSet != _dataSet || relation.ParentTable.DataSet != _dataSet) { throw ExceptionBuilder.ForeignRelation(); } relation.CheckState(); if (relation.Nested) { relation.CheckNestedRelations(); } if (relation._relationName.Length == 0) { relation._relationName = AssignName(); } else { RegisterName(relation._relationName); } DataKey childKey = relation.ChildKey; for (int i = 0; i < _relations.Count; i++) { if (childKey.ColumnsEqual(((DataRelation)_relations[i] !).ChildKey)) { if (relation.ParentKey.ColumnsEqual(((DataRelation)_relations[i] !).ParentKey)) { throw ExceptionBuilder.RelationAlreadyExists(); } } } _relations.Add(relation); ((DataTableRelationCollection)(relation.ParentTable.ChildRelations)).Add(relation); // Caching in ParentTable -> ChildRelations ((DataTableRelationCollection)(relation.ChildTable.ParentRelations)).Add(relation); // Caching in ChildTable -> ParentRelations relation.SetDataSet(_dataSet); relation.ChildKey.GetSortIndex().AddRef(); if (relation.Nested) { relation.ChildTable.CacheNestedParent(); } ForeignKeyConstraint?foreignKey = relation.ChildTable.Constraints.FindForeignKeyConstraint(relation.ParentColumnsReference, relation.ChildColumnsReference); if (relation._createConstraints) { if (foreignKey == null) { relation.ChildTable.Constraints.Add(foreignKey = new ForeignKeyConstraint(relation.ParentColumnsReference, relation.ChildColumnsReference)); // try to name the fk constraint the same as the parent relation: try { foreignKey.ConstraintName = relation.RelationName; } catch (Exception e) when(Common.ADP.IsCatchableExceptionType(e)) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } } } UniqueConstraint?key = relation.ParentTable.Constraints.FindKeyConstraint(relation.ParentColumnsReference); relation.SetParentKeyConstraint(key); relation.SetChildKeyConstraint(foreignKey); }
protected override void RemoveCore (DataRelation relation) { base.RemoveCore (relation); relation.SetDataSet (null); relation.ParentTable.ChildRelations.Remove (relation); relation.ChildTable.ParentRelations.Remove (relation); relation.SetParentKeyConstraint (null); relation.SetChildKeyConstraint (null); }
private void ProcessRelationIdentity (XmlSchemaElement element, ConstraintStructure c) { // Basic concept came from XmlSchemaMapper.cs string tableName = c.TableName; DataColumn [] cols; DataTable dt = dataset.Tables [tableName]; if (dt == null) throw new DataException (String.Format ("Invalid XPath selection inside selector. Cannot find: {0}", tableName)); cols = new DataColumn [c.Columns.Length]; for (int i = 0; i < cols.Length; i++) { string colName = c.Columns [i]; bool isAttr = c.IsAttribute [i]; DataColumn col = dt.Columns [colName]; if (isAttr && col.ColumnMapping != MappingType.Attribute) throw new DataException ("The XPath specified attribute field, but mapping type is not attribute."); if (!isAttr && col.ColumnMapping != MappingType.Element) throw new DataException ("The XPath specified simple element field, but mapping type is not simple element."); cols [i] = col; } string name = c.ReferName; // get the unique constraint for the releation UniqueConstraint uniq = FindConstraint (name, element); // generate the FK. ForeignKeyConstraint fkc = new ForeignKeyConstraint(c.ConstraintName, uniq.Columns, cols); dt.Constraints.Add (fkc); if (!c.IsConstraintOnly) { // generate the relation. DataRelation rel = new DataRelation (c.ConstraintName, uniq.Columns, cols, true); rel.Nested = c.IsNested; rel.SetParentKeyConstraint (uniq); rel.SetChildKeyConstraint (fkc); dataset.Relations.Add (rel); } }
protected override void AddCore(DataRelation relation) { base.AddCore(relation); if ((relation.ChildTable.DataSet != this.dataSet) || (relation.ParentTable.DataSet != this.dataSet)) { throw ExceptionBuilder.ForeignRelation(); } relation.CheckState(); if (relation.Nested) { relation.CheckNestedRelations(); } if (relation.relationName.Length == 0) { relation.relationName = base.AssignName(); } else { base.RegisterName(relation.relationName); } DataKey childKey = relation.ChildKey; for (int i = 0; i < this.relations.Count; i++) { if (childKey.ColumnsEqual(((DataRelation)this.relations[i]).ChildKey) && relation.ParentKey.ColumnsEqual(((DataRelation)this.relations[i]).ParentKey)) { throw ExceptionBuilder.RelationAlreadyExists(); } } this.relations.Add(relation); ((DataRelationCollection.DataTableRelationCollection)relation.ParentTable.ChildRelations).Add(relation); ((DataRelationCollection.DataTableRelationCollection)relation.ChildTable.ParentRelations).Add(relation); relation.SetDataSet(this.dataSet); relation.ChildKey.GetSortIndex().AddRef(); if (relation.Nested) { relation.ChildTable.CacheNestedParent(); } ForeignKeyConstraint constraint = relation.ChildTable.Constraints.FindForeignKeyConstraint(relation.ParentColumnsReference, relation.ChildColumnsReference); if (relation.createConstraints && (constraint == null)) { relation.ChildTable.Constraints.Add(constraint = new ForeignKeyConstraint(relation.ParentColumnsReference, relation.ChildColumnsReference)); try { constraint.ConstraintName = relation.RelationName; } catch (Exception exception) { if (!ADP.IsCatchableExceptionType(exception)) { throw; } ExceptionBuilder.TraceExceptionWithoutRethrow(exception); } } UniqueConstraint constraint2 = relation.ParentTable.Constraints.FindKeyConstraint(relation.ParentColumnsReference); relation.SetParentKeyConstraint(constraint2); relation.SetChildKeyConstraint(constraint); }
protected override void AddCore(DataRelation relation) { base.AddCore(relation); if (relation.ChildTable.DataSet != dataSet || relation.ParentTable.DataSet != dataSet) { throw ExceptionBuilder.ForeignRelation(); } relation.CheckState(); if (relation.Nested) { relation.CheckNestedRelations(); } if (relation.relationName.Length == 0) { relation.relationName = AssignName(); } else { RegisterName(relation.relationName); } DataKey childKey = relation.ChildKey; for (int i = 0; i < relations.Count; i++) { if (childKey.ColumnsEqual(((DataRelation)relations[i]).ChildKey)) { if (relation.ParentKey.ColumnsEqual(((DataRelation)relations[i]).ParentKey)) { throw ExceptionBuilder.RelationAlreadyExists(); } } } relations.Add(relation); ((DataRelationCollection.DataTableRelationCollection)(relation.ParentTable.ChildRelations)).Add(relation); // Caching in ParentTable -> ChildRelations ((DataRelationCollection.DataTableRelationCollection)(relation.ChildTable.ParentRelations)).Add(relation); // Caching in ChildTable -> ParentRelations relation.SetDataSet(dataSet); relation.ChildKey.GetSortIndex().AddRef(); if (relation.Nested) { relation.ChildTable.CacheNestedParent(relation); } ForeignKeyConstraint foreignKey = relation.ChildTable.Constraints.FindForeignKeyConstraint(relation.ParentColumns, relation.ChildColumns); if (relation.createConstraints) { if (foreignKey == null) { relation.ChildTable.Constraints.Add(foreignKey = new ForeignKeyConstraint(relation.ParentColumns, relation.ChildColumns)); // try to name the fk constraint the same as the parent relation: try { foreignKey.ConstraintName = relation.RelationName; } catch { // ignore the exception } } } UniqueConstraint key = relation.ParentTable.Constraints.FindKeyConstraint(relation.ParentColumns); relation.SetParentKeyConstraint(key); relation.SetChildKeyConstraint(foreignKey); }
protected override void AddCore(DataRelation relation) { base.AddCore(relation); if ((relation.ChildTable.DataSet != this.dataSet) || (relation.ParentTable.DataSet != this.dataSet)) { throw ExceptionBuilder.ForeignRelation(); } relation.CheckState(); if (relation.Nested) { relation.CheckNestedRelations(); } if (relation.relationName.Length == 0) { relation.relationName = base.AssignName(); } else { base.RegisterName(relation.relationName); } DataKey childKey = relation.ChildKey; for (int i = 0; i < this.relations.Count; i++) { if (childKey.ColumnsEqual(((DataRelation) this.relations[i]).ChildKey) && relation.ParentKey.ColumnsEqual(((DataRelation) this.relations[i]).ParentKey)) { throw ExceptionBuilder.RelationAlreadyExists(); } } this.relations.Add(relation); ((DataRelationCollection.DataTableRelationCollection) relation.ParentTable.ChildRelations).Add(relation); ((DataRelationCollection.DataTableRelationCollection) relation.ChildTable.ParentRelations).Add(relation); relation.SetDataSet(this.dataSet); relation.ChildKey.GetSortIndex().AddRef(); if (relation.Nested) { relation.ChildTable.CacheNestedParent(); } ForeignKeyConstraint constraint = relation.ChildTable.Constraints.FindForeignKeyConstraint(relation.ParentColumnsReference, relation.ChildColumnsReference); if (relation.createConstraints && (constraint == null)) { relation.ChildTable.Constraints.Add(constraint = new ForeignKeyConstraint(relation.ParentColumnsReference, relation.ChildColumnsReference)); try { constraint.ConstraintName = relation.RelationName; } catch (Exception exception) { if (!ADP.IsCatchableExceptionType(exception)) { throw; } ExceptionBuilder.TraceExceptionWithoutRethrow(exception); } } UniqueConstraint constraint2 = relation.ParentTable.Constraints.FindKeyConstraint(relation.ParentColumnsReference); relation.SetParentKeyConstraint(constraint2); relation.SetChildKeyConstraint(constraint); }