public ProfileThumbnail Load(ISqlConnectionInfo connection, SqlQueryParameters parameters)
        {
            IDatabase database = connection.Database;

            if (database == null)
            {
                throw new ArgumentNullException("database", "Error initializing database connection.");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            string sqlCmdText = string.Empty;

            try
            {
                sqlCmdText = "SELECT {0} " +
                             ProfileThumbnailTable.GetColumnNames("[pt]") +
                             (this.Depth > 0 ? "," + ProfileTable.GetColumnNames("[pt_p]") : string.Empty) +
                             (this.Depth > 1 ? "," + ProfileGroupTable.GetColumnNames("[pt_p_pg]") : string.Empty) +
                             " FROM [web].[ProfileThumbnail] AS [pt] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [web].[Profile] AS [pt_p] ON [pt].[ProfileID] = [pt_p].[ProfileID] ";
                }
                if (this.Depth > 1)
                {
                    sqlCmdText += "INNER  JOIN [web].[ProfileGroup] AS [pt_p_pg] ON [pt_p].[ProfileGroupID] = [pt_p_pg].[ProfileGroupID] ";
                }


                parameters.Top = 1;
                sqlCmdText     = parameters.BuildQuery(sqlCmdText);
                SqlCommand sqlCmd = database.Add(sqlCmdText) as SqlCommand;
                foreach (KeyValuePair <string, object> argument in parameters.Arguments)
                {
                    sqlCmd.Parameters.AddWithValue("@" + argument.Key, argument.Value);
                }

                SqlDataReader sqlReader = database.Add(sqlCmd) as SqlDataReader;

                if (!sqlReader.HasRows || !sqlReader.Read())
                {
                    IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("pt", "customload", "notfound"), "ProfileThumbnail could not be loaded using custom logic as it was not found.", sqlCmdText, this, connection, parameters);
                    if (this.Logger.IsDebugEnabled)
                    {
                        this.Logger.Debug(builder.ToString());
                    }
                    sqlReader.Close();
                    return(null);
                }

                SqlQuery query = new SqlQuery(sqlReader);

                ProfileThumbnailTable ptTable      = new ProfileThumbnailTable(query);
                ProfileTable          pt_pTable    = (this.Depth > 0) ? new ProfileTable(query) : null;
                ProfileGroupTable     pt_p_pgTable = (this.Depth > 1) ? new ProfileGroupTable(query) : null;


                ProfileGroup     pt_p_pgObject = (this.Depth > 1) ? pt_p_pgTable.CreateInstance() : null;
                Profile          pt_pObject    = (this.Depth > 0) ? pt_pTable.CreateInstance(pt_p_pgObject) : null;
                ProfileThumbnail ptObject      = ptTable.CreateInstance(pt_pObject);
                sqlReader.Close();

                return(ptObject);
            }
            catch (Exception ex)
            {
                database.HandleException(ex);
                IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("pt", "customload", "exception"), "ProfileThumbnail could not be loaded using custom logic. See exception for details.", sqlCmdText, ex, this, connection, parameters);
                if (this.Logger.IsErrorEnabled)
                {
                    this.Logger.Error(builder.ToString(), ex);
                }
                throw new DataOperationException(DataOperation.Load, "ProfileThumbnail", "Exception while loading (custom/single) ProfileThumbnail object from database. See inner exception for details.", ex);
            }
        }
        protected override ProfileThumbnail LoadInternal(ISqlConnectionInfo connection, int id)
        {
            IDatabase database = connection.Database;

            if (database == null)
            {
                throw new ArgumentNullException("database", "Error initializing database connection.");
            }
            string sqlCmdText = string.Empty;

            try
            {
                sqlCmdText = "SELECT " +
                             ProfileThumbnailTable.GetColumnNames("[pt]") +
                             (this.Depth > 0 ? "," + ProfileTable.GetColumnNames("[pt_p]") : string.Empty) +
                             (this.Depth > 1 ? "," + ProfileGroupTable.GetColumnNames("[pt_p_pg]") : string.Empty) +
                             " FROM [web].[ProfileThumbnail] AS [pt] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [web].[Profile] AS [pt_p] ON [pt].[ProfileID] = [pt_p].[ProfileID] ";
                }
                if (this.Depth > 1)
                {
                    sqlCmdText += "INNER  JOIN [web].[ProfileGroup] AS [pt_p_pg] ON [pt_p].[ProfileGroupID] = [pt_p_pg].[ProfileGroupID] ";
                }
                sqlCmdText += "WHERE [pt].[ProfileThumbnailID] = @ProfileThumbnailID;";

                SqlCommand sqlCmd = database.Add(sqlCmdText) as SqlCommand;
                sqlCmd.Parameters.AddWithValue("@ProfileThumbnailID", id);
                SqlDataReader sqlReader = database.Add(sqlCmd) as SqlDataReader;

                if (!sqlReader.HasRows || !sqlReader.Read())
                {
                    IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("pt", "loadinternal", "notfound"), "ProfileThumbnail could not be loaded by id as it was not found.", sqlCmdText, this, connection, id);
                    if (this.Logger.IsWarnEnabled)
                    {
                        this.Logger.Warn(builder.ToString());
                    }
                    sqlReader.Close();
                    return(null);
                }

                SqlQuery query = new SqlQuery(sqlReader);

                ProfileThumbnailTable ptTable      = new ProfileThumbnailTable(query);
                ProfileTable          pt_pTable    = (this.Depth > 0) ? new ProfileTable(query) : null;
                ProfileGroupTable     pt_p_pgTable = (this.Depth > 1) ? new ProfileGroupTable(query) : null;


                ProfileGroup     pt_p_pgObject = (this.Depth > 1) ? pt_p_pgTable.CreateInstance() : null;
                Profile          pt_pObject    = (this.Depth > 0) ? pt_pTable.CreateInstance(pt_p_pgObject) : null;
                ProfileThumbnail ptObject      = ptTable.CreateInstance(pt_pObject);
                sqlReader.Close();

                return(ptObject);
            }
            catch (Exception ex)
            {
                database.HandleException(ex);
                IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("pt", "loadinternal", "exception"), "ProfileThumbnail could not be loaded by id. See exception for details.", sqlCmdText, ex, this, connection, id);
                if (this.Logger.IsErrorEnabled)
                {
                    this.Logger.Error(builder.ToString(), ex);
                }
                throw new DataOperationException(DataOperation.Load, "ProfileThumbnail", "Exception while loading ProfileThumbnail object from database. See inner exception for details.", ex);
            }
        }