/// <include file='doc\ConstraintCollection.uex' path='docs/doc[@for="ConstraintCollection.Add"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Adds the constraint to the collection.</para>
        /// </devdoc>
        public void Add(Constraint constraint)
        {
            if (constraint == null)
            {
                throw ExceptionBuilder.ArgumentNull("constraint");
            }

            // It is an error if we find an equivalent constraint already in collection
            if (FindConstraint(constraint) != null)
            {
                throw ExceptionBuilder.DuplicateConstraint(FindConstraint(constraint).ConstraintName);
            }

            if (constraint is UniqueConstraint)
            {
                if (((UniqueConstraint)constraint).bPrimaryKey)
                {
                    if (Table.primaryKey != null)
                    {
                        throw ExceptionBuilder.AddPrimaryKeyConstraint();
                    }
                }
                AddUniqueConstraint((UniqueConstraint)constraint);
            }
            else if (constraint is ForeignKeyConstraint)
            {
                ForeignKeyConstraint fk  = (ForeignKeyConstraint)constraint;
                UniqueConstraint     key = fk.RelatedTable.Constraints.FindKeyConstraint(fk.RelatedColumns);
                if (key == null)
                {
                    if (constraint.ConstraintName.Length == 0)
                    {
                        constraint.ConstraintName = AssignName();
                    }
                    else
                    {
                        RegisterName(constraint.ConstraintName);
                    }

                    key = new UniqueConstraint(fk.RelatedColumns);
                    fk.RelatedTable.Constraints.Add(key);
                }
                AddForeignKeyConstraint((ForeignKeyConstraint)constraint);
            }

            BaseAdd(constraint);
            ArrayAdd(constraint);
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, constraint));


            if (constraint is UniqueConstraint)
            {
                if (((UniqueConstraint)constraint).bPrimaryKey)
                {
                    Table.PrimaryKey = ((UniqueConstraint)constraint).Columns;
                }
            }
        }
Beispiel #2
0
 internal void Add(Constraint constraint, bool addUniqueWhenAddingForeign)
 {
     if (constraint == null)
     {
         throw ExceptionBuilder.ArgumentNull("constraint");
     }
     if (this.FindConstraint(constraint) != null)
     {
         throw ExceptionBuilder.DuplicateConstraint(this.FindConstraint(constraint).ConstraintName);
     }
     if ((1 < this.table.NestedParentRelations.Length) && !this.AutoGenerated(constraint))
     {
         throw ExceptionBuilder.CantAddConstraintToMultipleNestedTable(this.table.TableName);
     }
     if (constraint is UniqueConstraint)
     {
         if (((UniqueConstraint)constraint).bPrimaryKey && (this.Table.primaryKey != null))
         {
             throw ExceptionBuilder.AddPrimaryKeyConstraint();
         }
         this.AddUniqueConstraint((UniqueConstraint)constraint);
     }
     else if (constraint is ForeignKeyConstraint)
     {
         ForeignKeyConstraint constraint2 = (ForeignKeyConstraint)constraint;
         if (addUniqueWhenAddingForeign && (constraint2.RelatedTable.Constraints.FindKeyConstraint(constraint2.RelatedColumnsReference) == null))
         {
             if (constraint.ConstraintName.Length == 0)
             {
                 constraint.ConstraintName = this.AssignName();
             }
             else
             {
                 this.RegisterName(constraint.ConstraintName);
             }
             UniqueConstraint constraint3 = new UniqueConstraint(constraint2.RelatedColumnsReference);
             constraint2.RelatedTable.Constraints.Add(constraint3);
         }
         this.AddForeignKeyConstraint((ForeignKeyConstraint)constraint);
     }
     this.BaseAdd(constraint);
     this.ArrayAdd(constraint);
     this.OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, constraint));
     if ((constraint is UniqueConstraint) && ((UniqueConstraint)constraint).bPrimaryKey)
     {
         this.Table.PrimaryKey = ((UniqueConstraint)constraint).ColumnsReference;
     }
 }
        // To add foreign key constraint without adding any unique constraint for internal use. Main purpose : Binary Remoting
        internal void Add(Constraint constraint, bool addUniqueWhenAddingForeign)
        {
            if (constraint == null)
            {
                throw ExceptionBuilder.ArgumentNull(nameof(constraint));
            }

            // It is an error if we find an equivalent constraint already in collection
            if (FindConstraint(constraint) != null)
            {
                throw ExceptionBuilder.DuplicateConstraint(FindConstraint(constraint).ConstraintName);
            }

            if (1 < _table.NestedParentRelations.Length)
            {
                if (!AutoGenerated(constraint))
                {
                    throw ExceptionBuilder.CantAddConstraintToMultipleNestedTable(_table.TableName);
                }
            }

            if (constraint is UniqueConstraint)
            {
                if (((UniqueConstraint)constraint)._bPrimaryKey)
                {
                    if (Table._primaryKey != null)
                    {
                        throw ExceptionBuilder.AddPrimaryKeyConstraint();
                    }
                }
                AddUniqueConstraint((UniqueConstraint)constraint);
            }
            else if (constraint is ForeignKeyConstraint)
            {
                ForeignKeyConstraint fk = (ForeignKeyConstraint)constraint;
                if (addUniqueWhenAddingForeign)
                {
                    UniqueConstraint key = fk.RelatedTable.Constraints.FindKeyConstraint(fk.RelatedColumnsReference);
                    if (key == null)
                    {
                        if (constraint.ConstraintName.Length == 0)
                        {
                            constraint.ConstraintName = AssignName();
                        }
                        else
                        {
                            RegisterName(constraint.ConstraintName);
                        }

                        key = new UniqueConstraint(fk.RelatedColumnsReference);
                        fk.RelatedTable.Constraints.Add(key);
                    }
                }
                AddForeignKeyConstraint((ForeignKeyConstraint)constraint);
            }
            BaseAdd(constraint);
            ArrayAdd(constraint);
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, constraint));

            if (constraint is UniqueConstraint)
            {
                if (((UniqueConstraint)constraint)._bPrimaryKey)
                {
                    Table.PrimaryKey = ((UniqueConstraint)constraint).ColumnsReference;
                }
            }
        }