System.Data.DataRelationCollection.DataSetRelationCollection.AddCore C# (CSharp) Метод

AddCore() защищенный Метод

protected AddCore ( DataRelation relation ) : void
relation DataRelation
Результат void
            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);
            }