/// <summary> /// Gets the child related data. /// </summary> /// <param name="childTableType">Type of the child table.</param> /// <param name="relation">The relation.</param> /// <returns></returns> protected TableMetadata[] GetChildRelatedTableData(Type childTableType, ChildTableRelation relation) { PersistentObject persistent = null; try { //create a instance of the table so we can check the relations between out table and this TableMetadata childTable = (TableMetadata)Activator.CreateInstance(childTableType); object value = GetField(relation.ForeignKeyName).fieldValue; //we have the relation between the 2 tables. persistent = new PersistentObject(this); TableMetadata[] data = (TableMetadata[])persistent.GetTableMetadata(relation.RelatedTableName, childTableType, value); return(data); } catch { throw; } finally { if (persistent != null) { persistent.Dispose(); } } }
/// <summary> /// Returns data from a related table in Parent -> Child relationship. This is /// the underlying implementation of the generated GetXXX tables. /// </summary> /// <param name="childTableType">Type of the related entity</param> /// <returns>Array with the result</returns> protected TableMetadata[] GetRelatedTableData(Type childTableType) { PersistentObject persistent = null; TableRelation relation = null; try { //create a instance of the table so we can check the relations between out table and this TableMetadata childTable = (TableMetadata)Activator.CreateInstance(childTableType); //first check the fieldValue of the primary key if (GetPrimaryKeyField().fieldValue == null) { throw new InvalidOperationException("The primary key does not have a fieldValue"); } //get the relation foreach (TableRelation var in Relations) { if (var.RelatedTableName == childTable.TableName) { relation = var; break; } } if (relation is ChildTableRelation) { return(GetChildRelatedTableData(childTableType, (ChildTableRelation)relation)); } //check if we got the relation if (relation == null) { throw new ArgumentException("A relation cannot be found between the tables"); } //we have the relation between the 2 tables. persistent = new PersistentObject(this); TableMetadata[] data = null; if (relation.RelationCardinality == TableRelationCardinality.OneToOne) { ParentTableRelation pr = (ParentTableRelation)(relation); //take the fk value data = (TableMetadata[])persistent.GetTableMetadata(relation.RelatedTableName, childTableType, GetField(pr.ForeignKeyName).fieldValue); } else { //take the pk value data = (TableMetadata[])persistent.GetTableMetadata(relation.RelatedTableName, childTableType, GetPrimaryKeyField().fieldValue); } return(data); } catch { throw; } }