Exemplo n.º 1
0
        /// <summary>
        /// Create a <see cref="ForeignKey"/> for the columns in the Table.
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="keyColumns">An <see cref="IList"/> of <see cref="Column"/> objects.</param>
        /// <param name="referencedEntityName"></param>
        /// <param name="referencedColumns"></param>
        /// <returns>
        /// A <see cref="ForeignKey"/> for the columns in the Table.
        /// </returns>
        /// <remarks>
        /// This does not necessarily create a <see cref="ForeignKey"/>, if
        /// one already exists for the columns then it will return an
        /// existing <see cref="ForeignKey"/>.
        /// </remarks>
        public virtual ForeignKey CreateForeignKey(string keyName, IEnumerable <Column> keyColumns, string referencedEntityName,
                                                   IEnumerable <Column> referencedColumns)
        {
            IEnumerable <Column> kCols   = keyColumns;
            IEnumerable <Column> refCols = referencedColumns;

            var key = new ForeignKeyKey(kCols, referencedEntityName, refCols);

            ForeignKey fk;

            foreignKeys.TryGetValue(key, out fk);

            if (fk == null)
            {
                fk = new ForeignKey();
                // NOTE : if the name is null, we will generate an implicit name during second pass processing
                // after we know the referenced table name (which might not be resolved yet).
                fk.Name  = keyName;
                fk.Table = this;
                foreignKeys.Add(key, fk);
                fk.ReferencedEntityName = referencedEntityName;
                fk.AddColumns(kCols);
                if (referencedColumns != null)
                {
                    fk.AddReferencedColumns(refCols);
                }
            }

            if (!string.IsNullOrEmpty(keyName))
            {
                fk.Name = keyName;
            }

            return(fk);
        }
		public void ToStringDoesNotThrow()
		{
			var key = new ForeignKey
				{
				Table = new Table("TestTable"),
				Name = "TestForeignKey"
			};
			key.AddColumn(new Column("TestColumn"));
			key.AddReferencedColumns(new[] { new Column("ReferencedColumn") });

			string toString = null;
			Assert.DoesNotThrow(() =>
				{
					toString = key.ToString();
				});

			Assert.That(toString, Is.EqualTo("NHibernate.Mapping.ForeignKey(TestTableNHibernate.Mapping.Column(TestColumn) ref-columns:(NHibernate.Mapping.Column(ReferencedColumn)) as TestForeignKey"));
		}
Exemplo n.º 3
0
        /// <summary>
        /// Create a <see cref="ForeignKey"/> for the columns in the Table.
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="keyColumns">An <see cref="IList"/> of <see cref="Column"/> objects.</param>
        /// <param name="referencedEntityName"></param>
        /// <param name="referencedColumns"></param>
        /// <returns>
        /// A <see cref="ForeignKey"/> for the columns in the Table.
        /// </returns>
        /// <remarks>
        /// This does not necessarily create a <see cref="ForeignKey"/>, if
        /// one already exists for the columns then it will return an
        /// existing <see cref="ForeignKey"/>.
        /// </remarks>
        public virtual ForeignKey CreateForeignKey(string keyName, IEnumerable <Column> keyColumns, string referencedEntityName,
                                                   IEnumerable <Column> referencedColumns)
        {
            IEnumerable <Column> kCols   = keyColumns;
            IEnumerable <Column> refCols = referencedColumns;

            var key = new ForeignKeyKey(kCols, referencedEntityName, refCols);

            ForeignKey fk;

            foreignKeys.TryGetValue(key, out fk);

            if (fk == null)
            {
                fk = new ForeignKey();
                if (!string.IsNullOrEmpty(keyName))
                {
                    fk.Name = keyName;
                }
                else
                {
                    fk.Name = "FK" + UniqueColumnString(kCols, referencedEntityName);
                    //TODO: add referencedClass to disambiguate to FKs on the same columns, pointing to different tables
                }
                fk.Table = this;
                foreignKeys.Add(key, fk);
                fk.ReferencedEntityName = referencedEntityName;
                fk.AddColumns(kCols);
                if (referencedColumns != null)
                {
                    fk.AddReferencedColumns(refCols);
                }
            }

            if (!string.IsNullOrEmpty(keyName))
            {
                fk.Name = keyName;
            }

            return(fk);
        }