private void CreateHasManyRelations(ActiveRecordDescriptor desc, TableDefinition tableDef,
                                            IList list, BuildContext context)
        {
            foreach (TableDefinition fkTable in tableDef.TablesReferencedByHasRelation)
            {
                String propertyName = _namingService.CreateRelationName(fkTable.Name);
                ActiveRecordDescriptor targetType = null;
                String           colName          = null;
                bool             pendentNecessary = false;
                ColumnDefinition selectedCol      = null;

                foreach (ColumnDefinition col in fkTable.Columns)
                {
                    if (col.RelatedTable == tableDef)
                    {
                        colName = col.Name;

                        if (col.RelatedTable.RelatedDescriptor == null && col.RelatedTable != fkTable)
                        {
                            col.RelatedTable.RelatedDescriptor = new ActiveRecordDescriptor(fkTable);

                            pendentNecessary = true;
                        }
                        else if (col.RelatedTable == tableDef)
                        {
                            targetType = desc;
                        }

                        if (targetType == null)
                        {
                            targetType = col.RelatedTable.RelatedDescriptor;
                        }

                        selectedCol = col;

                        break;
                    }
                }

                // Just to protect ourselves from awkward conditions
                if (colName == null)
                {
                    continue;
                }

                ActiveRecordHasManyDescriptor hasMany =
                    new ActiveRecordHasManyDescriptor(colName, propertyName, targetType);

                if (pendentNecessary)
                {
                    context.AddPendentDescriptor(hasMany, selectedCol.RelatedTable.RelatedDescriptor);
                }

                list.Add(hasMany);
            }
        }
        private void CreateBelongsToRelations(ActiveRecordDescriptor desc, TableDefinition tableDef,
                                              IList list, BuildContext context)
        {
            foreach (ColumnDefinition col in tableDef.Columns)
            {
                if (col.RelatedTable != null)
                {
                    bool   pendentNecessary = false;
                    String propertyName     = _namingService.CreateClassName(col.RelatedTable.Name);

                    ActiveRecordDescriptor targetType = null;

                    if (col.RelatedTable.RelatedDescriptor == null && col.RelatedTable != tableDef)
                    {
                        col.RelatedTable.RelatedDescriptor = new ActiveRecordDescriptor(col.RelatedTable);

                        pendentNecessary = true;
                    }
                    else if (col.RelatedTable == tableDef)
                    {
                        targetType = desc;
                    }

                    if (targetType == null)
                    {
                        targetType = col.RelatedTable.RelatedDescriptor;
                    }

                    ActiveRecordBelongsToDescriptor belongsTo =
                        new ActiveRecordBelongsToDescriptor(col.Name,
                                                            propertyName, targetType);

                    list.Add(belongsTo);

                    if (pendentNecessary)
                    {
                        context.AddPendentDescriptor(belongsTo, col.RelatedTable.RelatedDescriptor);
                    }
                }
            }
        }
		private void CreateHasManyRelations(ActiveRecordDescriptor desc, TableDefinition tableDef, 
			IList list, BuildContext context)
		{
			foreach(TableDefinition fkTable in tableDef.TablesReferencedByHasRelation)
			{
				String propertyName = _namingService.CreateRelationName(fkTable.Name);
				ActiveRecordDescriptor targetType = null;
				String colName = null;
				bool pendentNecessary = false;
				ColumnDefinition selectedCol = null;

				foreach(ColumnDefinition col in fkTable.Columns)
				{
					if (col.RelatedTable == tableDef)
					{
						colName = col.Name;

						if (col.RelatedTable.RelatedDescriptor == null && col.RelatedTable != fkTable)
						{
							col.RelatedTable.RelatedDescriptor = new ActiveRecordDescriptor(fkTable);

							pendentNecessary = true;
						}
						else if (col.RelatedTable == tableDef)
						{
							targetType = desc;
						}
						
						if (targetType == null)
						{
							targetType = col.RelatedTable.RelatedDescriptor;
						}

						selectedCol = col;

						break;
					}
				}

				// Just to protect ourselves from awkward conditions
				if (colName == null) continue;

				ActiveRecordHasManyDescriptor hasMany = 
					new ActiveRecordHasManyDescriptor(colName, propertyName, targetType);

				if (pendentNecessary)
				{
					context.AddPendentDescriptor(hasMany, selectedCol.RelatedTable.RelatedDescriptor);
				}
				
				list.Add(hasMany);
			}
		}
		private void CreateBelongsToRelations(ActiveRecordDescriptor desc, TableDefinition tableDef, 
			IList list, BuildContext context)
		{
			foreach(ColumnDefinition col in tableDef.Columns)
			{
				if (col.RelatedTable != null)
				{
					bool pendentNecessary = false;
					String propertyName = _namingService.CreateClassName(col.RelatedTable.Name);

					ActiveRecordDescriptor targetType = null;

					if (col.RelatedTable.RelatedDescriptor == null && col.RelatedTable != tableDef)
					{
						col.RelatedTable.RelatedDescriptor = new ActiveRecordDescriptor(col.RelatedTable);

						pendentNecessary = true;
					}
					else if (col.RelatedTable == tableDef)
					{
						targetType = desc;
					}

					if (targetType == null)
					{
						targetType = col.RelatedTable.RelatedDescriptor;
					}

					ActiveRecordBelongsToDescriptor belongsTo = 
						new ActiveRecordBelongsToDescriptor(col.Name, 
							propertyName, targetType);

					list.Add(belongsTo);

					if (pendentNecessary)
					{
						context.AddPendentDescriptor(belongsTo, col.RelatedTable.RelatedDescriptor);
					}
				}
			}
		}