protected ICollection <IReadOnlyEntity> ReadRelationChildrenFromDb(IReadOnlyEntity entity, Type entityType , ITransaction tx, IRelation relation) { var retrievedEntities = new List <IReadOnlyEntity>(); ICollection <Type> childTypesToProcess = GetChildTypesToProcess(relation); int index = 0; foreach (var childType in childTypesToProcess) { index++; IRelation effectiveRelation = relation.Clone(); effectiveRelation.RelatedObjectType = childType; effectiveRelation.RelationShipName = relation.RelationShipName + "_" + index; EntityInfo entityInfo = CacheManager.GetEntityInfo(entityType); var logSb = new StringBuilder(); var query = entityInfo.GetRelationObjectLoad(DbLayer, effectiveRelation); IList <string> fields = new List <string>(); foreach (RelationColumnMapping mapping in effectiveRelation.TableColumnMappings) { fields.Add(mapping.FromField); } IDbCommand cmd = null; try { cmd = tx.CreateCommand(); cmd.CommandText = query; } catch (Exception ex) { string message = String.Format("SQL Exception while trying create command for sql {0}", query); throw new CommandCreationException(message, ex); } bool showQuery = Config.ShowQueries; if (showQuery) { logSb.Append(query); } for (int i = 0; i < fields.Count; i++) { string field = fields[i]; object fieldValue = null; IColumn matchColumn = entityInfo.FindColumnByAttribute(field); EntityRelationColumnInfo entityRelationColumnInfo = entityInfo.FindRelationColumnInfo(matchColumn != null ? matchColumn.AttributeName : ""); if (entityRelationColumnInfo != null) { EntityFieldValue entityFieldValue = entity.Context.ChangeTracker.GetFieldValue(matchColumn.AttributeName); fieldValue = entityFieldValue.Value; } else if (matchColumn != null) { PropertyInfo getter = entityInfo.GetProperty(matchColumn.AttributeName); fieldValue = ReflectionUtils.GetValue(getter, entity); } else { string message = String.Format("The field {0} does not have a matching field in the object {1}" , field, entity.GetType().FullName); throw new NoMatchingColumnFoundException(message); } if (showQuery) { logSb.Append(" ,").Append(matchColumn.ColumnName).Append("=").Append(fieldValue); } DbLayer.DataManipulate().SetToPreparedStatement(cmd, fieldValue, i + 1, matchColumn); } if (showQuery) { Logger.GetLogger(Config.LoggerName).Debug(logSb.ToString()); } if (Config.EnableStatistics) { Statistics.RegisterSelect(childType); } ICollection <IReadOnlyEntity> retrievedEntitiesForType = ExecuteAndReadFromPreparedStatement(entity, tx, cmd, childType); retrievedEntities.AddRange(retrievedEntitiesForType); } return(retrievedEntities); }