/// <summary>Sets the value of a field in the database. This overload allows to specify whether the value is set in the master table, or the target table.</summary>
        /// <typeparam name="TField">The type of the field.</typeparam>
        /// <param name="fieldName">Field name</param>
        /// <param name="value">New value</param>
        /// <param name="mode">Specifies whether we want to access the current (link) table ("Current") or the target table.</param>
        /// <param name="forceUpdate">
        ///     Should the value be set, even if it is the same as before (and therefore set the dirty flag
        ///     despite that there were no changes)?
        /// </param>
        /// <returns>True or False</returns>
        protected virtual bool WriteFieldValue <TField>(string fieldName, TField value, XLinkItemAccessMode mode, bool forceUpdate = false)
        {
            if (mode == XLinkItemAccessMode.CurrentTable)
            {
                return(SetFieldValue(fieldName, value, forceUpdate));
            }

            BusinessEntityHelper.CheckColumn(CurrentTargetRow.Table, fieldName);
            if (!(ParentEntity is BusinessEntity entity))
            {
                throw new NullReferenceException("Parent entity is not a business entity.");
            }
            return(BusinessEntityHelper.SetFieldValue(entity, fieldName, value, CurrentTargetRow.Table.TableName, CurrentRow.Table.DataSet, CurrentTargetRow, forceUpdate));
        }
        /// <summary>
        /// Sets the value of a field in the database
        /// </summary>
        /// <typeparam name="TField">The type of the field.</typeparam>
        /// <param name="fieldName">Field name</param>
        /// <param name="value">New value</param>
        /// <param name="forceUpdate">Should the value be set, even if it is the same as before (and therefore set the dirty flag despite that there were no changes)?</param>
        /// <param name="currentRow">Row that has the field that needs to be updated</param>
        /// <returns>True or False</returns>
        protected virtual bool WriteFieldValue <TField>(string fieldName, TField value, bool forceUpdate, DataRow currentRow)
        {
            if (!(ParentEntity is BusinessEntity entity))
            {
                throw new NullReferenceException("Parent entity is not a business entity.");
            }
            var retVal = BusinessEntityHelper.SetFieldValue(entity, fieldName, value, TableName, currentRow.Table.DataSet, currentRow, forceUpdate);

            entity.DataUpdated(fieldName, currentRow.Table.TableName);
            if (ParentCollection is EntitySubItemCollection collection)
            {
                collection.DataUpdated(fieldName, currentRow);
            }
            return(retVal);
        }
        /// <summary>
        /// Sets the value of a field in the database in the table specified.
        /// The record of the table is identified by the search expression
        /// </summary>
        /// <param name="fieldName">Field name</param>
        /// <param name="value">New value</param>
        /// <param name="forceUpdate">Should the value be set, even if it is the same as before (and therefore set the dirty flag despite that there were no changes)?</param>
        /// <param name="tableName">Name of the table that holds the row that needs to be updated</param>
        /// <param name="searchExpression">Search expression used to identify the record that needs to be updated</param>
        /// <returns>True or False</returns>
        /// <example>SetFieldValue("MyField","xxx value",true,"MySecondaryTable","id = 'x'");</example>
        protected virtual bool SetFieldValue(string fieldName, object value, bool forceUpdate, string tableName, string searchExpression)
        {
            if (!(ParentEntity is BusinessEntity entity))
            {
                throw new NullReferenceException("Parent entity is not a business entity.");
            }

            // TODO: We should probably do something with the search expression, so that can work too with maps
            fieldName = entity.GetInternalFieldName(fieldName, tableName);
            tableName = entity.GetInternalTableName(tableName);

            // Before we can do anything else, we have to retrieve the appropriate table
            if (!CurrentRow.Table.DataSet.Tables.Contains(tableName))
            {
                // The specified table name does not exist. We need to throw an error!
                throw new ArgumentException("Table '@tableName' not in DataSet", "tableName");
            }
            var secondaryTable = CurrentRow.Table.DataSet.Tables[tableName];

            // Now that we have the table, we can try to find the desired row
            var matchingRows = secondaryTable.Select(searchExpression);

            // We expect to find exactly one row. If fewer or more rows get returned, we throw an error
            if (matchingRows.Length != 1)
            {
                if (matchingRows.Length > 1)
                {
                    // The record was not uniquely identified
                    throw new ArgumentException("Unable to find unique record: " + matchingRows.Length.ToString(NumberFormatInfo.InvariantInfo) + " records returned by search expression '@searchExpression'", "searchExpression");
                }
                throw new RowNotInTableException("Search record not found by expression '@searchExpression'");
            }

            // We did find the row. We can now make sure it has the field we desire
            CheckColumn(fieldName, secondaryTable);

            var retVal = BusinessEntityHelper.SetFieldValue(entity, fieldName, value, tableName, matchingRows[0].Table.DataSet, matchingRows[0], forceUpdate);

            entity.DataUpdated(fieldName, CurrentRow.Table.TableName);

            if (ParentCollection is EntitySubItemCollection collection)
            {
                collection.DataUpdated(fieldName, matchingRows[0]);
            }
            return(retVal);
        }