Esempio n. 1
0
        internal override bool IsConstraintViolated()
        {
            if (Table.DataSet == null || RelatedTable.DataSet == null)
            {
                return(false);
            }

            bool hasErrors = false;

            foreach (DataRow row in Table.Rows)
            {
                if (row.RowState == DataRowState.Deleted)
                {
                    continue;
                }

                // we check if all values in _childColumns place are nulls.
                // if yes we return.
                if (row.IsNullColumns(_childColumns))
                {
                    continue;
                }

                // check whenever there is (at least one) parent row  in RelatedTable
                if (!RelatedTable.RowsExist(_parentColumns, _childColumns, row))
                {
                    // if no parent row exists - constraint is violated
                    hasErrors = true;
                    string[] values = new string[_childColumns.Length];
                    for (int i = 0; i < _childColumns.Length; i++)
                    {
                        DataColumn col = _childColumns[i];
                        values[i] = row[col].ToString();
                    }

                    row.RowError = String.Format("ForeignKeyConstraint {0} requires the child key values ({1}) to exist in the parent table.",
                                                 ConstraintName, String.Join(",", values));
                }
            }

            if (hasErrors)
            {
                //throw new ConstraintException("Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.");
                return(true);
            }

            return(false);
        }
        internal override void AssertConstraint(DataRow row)
        {
            // first we check if all values in _childColumns place are nulls.
            // if yes we return.
            if (row.IsNullColumns(_childColumns))
            {
                return;
            }

            // check whenever there is (at least one) parent row  in RelatedTable
            if (!RelatedTable.RowsExist(_parentColumns, _childColumns, row))
            {
                // if no parent row exists - constraint is violated
                throw new InvalidConstraintException(GetErrorMessage(row));
            }
        }