public void UnmatchingColumns()
		{
			Table primaryTable = new Table("pktable");
			primaryTable.PrimaryKey = new PrimaryKey();
			SimpleValue sv = new SimpleValue();
			sv.TypeName = NHibernateUtil.Int16.Name;
			Column pkColumn = new Column("pk_column");
			pkColumn.Value = sv;

			primaryTable.PrimaryKey.AddColumn(pkColumn);

			Table fkTable = new Table("fktable");

			ForeignKey fk = new ForeignKey();
			sv = new SimpleValue();
			sv.TypeName = NHibernateUtil.Int16.Name;
			Column fkColumn1 = new Column("col1");
			fkColumn1.Value = sv;

			sv = new SimpleValue();
			sv.TypeName = NHibernateUtil.Int16.Name;
			Column fkColumn2 = new Column("col2");
			fkColumn2.Value = sv;

			fk.AddColumn(fkColumn1);
			fk.AddColumn(fkColumn2);

			fk.Table = fkTable;

			fk.ReferencedTable = primaryTable;
			Assert.Throws<FKUnmatchingColumnsException>(() => fk.AlignColumns());
		}
		public void UnmatchingColumns()
		{

			Table primaryTable = new Table();
			primaryTable.Name = "pktable";
			primaryTable.PrimaryKey = new PrimaryKey();
			Column pkColumn = new Column( NHibernateUtil.Int16, 0 );
			pkColumn.Name = "pk_column";

			primaryTable.PrimaryKey.AddColumn( pkColumn );

			Table fkTable = new Table();
			fkTable.Name = "fktable";

			ForeignKey fk = new ForeignKey();
			Column fkColumn1 = new Column( NHibernateUtil.Int16, 0 );
			fkColumn1.Name = "col1";

			Column fkColumn2 = new Column( NHibernateUtil.Int16, 0 );
			fkColumn2.Name = "col2";

			fk.AddColumn( fkColumn1 );
			fk.AddColumn( fkColumn2 );
			
			fk.Table = fkTable;

			fk.ReferencedTable = primaryTable;

		}
Esempio n. 3
0
		internal ForeignKeyInfo(Table table, ForeignKey fk, Configuration config)
			: base("FK_", table.Name, fk.ColumnIterator, null)
		{
			//note: the fk object has a ReferencedTable property, but it doesn't always seem to be set
			//the reference class property is always set, so we use it instead to get the referenced table 
			Table referencedTable = config.GetClassMapping(fk.ReferencedEntityName).Table;
			_referencedTable = referencedTable.Name;
			_referencedColumns = CollectionUtils.Map<Column, string>(
                referencedTable.PrimaryKey.ColumnIterator,
				delegate(Column column) { return column.Name; });
		}
		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"));
		}
		private string GetForeignKeyName(ForeignKey fk)
		{
			// (the FKName length, of H3.2 implementation, may be too long for some RDBMS so we implement something different) 
			int hash = fk.Name.GetHashCode() ^ Name.GetHashCode();
			return string.Format("KF{0}", hash.ToString("X"));
		}
 public ForeignKeyColumn Create(ForeignKey foreignKey)
 {
     var fk= new ForeignKeyColumn(foreignKey);
     fk.OwnerTable = OwnerTable;
     return fk;
 }
		/// <summary>
		/// Create a <see cref="ForeignKey"/> for the columns in the Table.
		/// </summary>
		/// <param name="keyName"></param>
		/// <param name="columns">An <see cref="IList"/> of <see cref="Column"/> objects.</param>
		/// <param name="referencedClass"></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 ForeignKey CreateForeignKey( string keyName, IList columns, System.Type referencedClass )
		{
			if( keyName == null )
			{
				keyName = "FK" + UniqueColumnString( columns );
			}
			ForeignKey fk = ( ForeignKey ) foreignKeys[ keyName ];

			if( fk == null )
			{
				fk = new ForeignKey();
				fk.Name = keyName;
				fk.Table = this;
				fk.ReferencedClass = referencedClass;
				foreignKeys.Add( keyName, fk );
				foreach( Column col in columns )
				{
					fk.AddColumn( col );
				}
			}
			else
			{
				// "X"= hexadecimal format
				if( fk.ReferencedClass != referencedClass )
				{
					foreignKeys.Remove( keyName );
					keyName += referencedClass.Name.GetHashCode().ToString( "X" ).ToUpper( System.Globalization.CultureInfo.InvariantCulture );
					return CreateForeignKey( keyName, columns, referencedClass );
				}
			}
			return fk;
		}
 public ForeignKeyColumn(ForeignKey foreignKey)
     : base(foreignKey.Columns.First())
 {
     ReferencedTableName = foreignKey.ReferencedTable.Name;
 }