/// <summary>
        /// Returns the hierarchical object type of a foreign key table relation
        /// for up stream hierarchical properties.
        /// </summary>
        /// <remarks>
        /// For example, UpToEmployeesByReportsTo has an object type of
        /// Employees.
        /// </remarks>
        public string UpStreamObjectType(TableRelation tr)
        {
            if (tr.IsOneToOne && tr.IsDirect)
            {
                return this.Entity(tr.ForeignTable);
            }

            if (tr.IsManyToMany)
            {
                if (tr.ForeignTable.PrimaryKeys.Count == 2)
                {
                    return this.Entity(tr.CrossReferenceTable);
                }
            }

            if (tr.IsManyToOne)
            {
                return this.Entity(tr.ForeignTable);
            }

            return null;
        }
        /// <summary>
        /// Returns the hierarchical object type of a foreign key table relation
        /// for down stream hierarchical properties.
        /// </summary>
        /// <remarks>
        /// For example, EmployeesColectionByReportsTo has an object type of
        /// Employees.
        /// </remarks>
        public string DownStreamObjectType(TableRelation tr)
        {
            if (tr.IsOneToOne && !tr.IsDirect)
            {
                return this.Entity(tr.ForeignTable);
            }

            if (tr.IsZeroToMany)
            {
                return this.Entity(tr.ForeignTable);
            }

            return null;
        }
        /// <summary>
        /// Returns the hierarchical object name of a foreign key table relation
        /// for the up stream hierarchical property
        /// </summary>
        /// <remarks>
        /// For example, UpToEmployeesByReportsTo.
        /// </remarks>
        public string UpStreamObjectName(TableRelation tr)
        {
            if (tr.IsOneToOne && tr.IsDirect)
            {
                return this.sUpToPrefix + this.EntityRelationName(tr.ForeignTable);
            }

            if (tr.IsManyToMany)
            {
                if (tr.ForeignTable.PrimaryKeys.Count == 2)
                {
                    return this.sUpToPrefix + this.CollectionRelationName(tr.CrossReferenceTable, tr.ForeignTable);
                }
            }

            if (tr.IsManyToOne)
            {
                return this.sUpToPrefix + this.EntityRelationName(tr.ForeignTable, tr.PrimaryColumns[0], tr.IsSelfReference);
            }

            return null;
        }
        /// <summary>
        /// Returns the hierarchical object name of a foreign key table relation
        /// for the down stream hierarchical property
        /// </summary>
        /// <remarks>
        /// For example, EmployeesCollectionByReportsTo.
        /// </remarks>
        public string DownStreamObjectName(TableRelation tr)
        {
            if (tr.IsOneToOne && !tr.IsDirect)
            {
                return this.EntityRelationName(tr.ForeignTable);
            }

            if (tr.IsZeroToMany)
            {
                if (tr.IsSelfReference)
                {
                    return this.CollectionRelationName(tr.ForeignTable, tr.PrimaryColumns[0], tr.IsSelfReference);
                }
                else
                {
                    return this.CollectionRelationName(tr.ForeignTable, tr.ForeignColumns[0], tr.IsSelfReference);
                }
            }

            return null;
        }
        /// <summary>
        /// Returns true if the TableRelation for the foreign key
        /// is down stream from current table. 
        /// </summary>
        /// <remarks>
        /// True for zero to many.
        /// True for one side of a one to one, where
        /// the foreign table is the one with the FK constraint defined.
        /// </remarks>
        public bool IsDownStream(TableRelation tr)
        {
            if (tr.IsOneToOne && !tr.IsDirect)
            {
                return true;
            }

            if (tr.IsZeroToMany)
            {
                return true;
            }

            return false;
        }
 /// <summary>
 /// EXPERIMENTAL!
 /// This creates an instance of the TableRelation Class
 /// so that it can be accessed in templates using
 /// Microsoft script (VBScript or JScript.)
 /// </summary>
 /// <remarks>
 /// THIS IS EXPERIMENTAL!
 /// As yet, it does not work for VBScript.
 /// </remarks>
 /// <example>
 /// In JScript:
 /// <code>
 /// var tableRel = DnpUtils.CreateTableRelation(tableMeta, foreignKey);
 /// </code>
 /// In VBScript:
 /// <code>
 /// Dim tableRel
 /// Set tableRel = DnpUtils.CreateTableRelation(tableMeta, foreignKey)
 /// </code>
 /// </example>
 public TableRelation CreateTableRelation(ITable tableMeta, IForeignKey foreignKey)
 {
     TableRelation tr = new TableRelation(tableMeta, foreignKey);
     return tr;
 }