/// <summary>
        /// Saves data from changed fields in JSON.
        /// Enables to track entity history.
        /// </summary>
        private void AuditInfo(IEntity2 entity, NinjaSoftware.Enio.CoolJ.Enums.AuditInfoActionTypeEnum auditInfoActionTypeEnum)
        {
            NinjaSoftware.Enio.CoolJ.EntityClasses.AuditInfoEntity auditInfo = new NinjaSoftware.Enio.CoolJ.EntityClasses.AuditInfoEntity();
            auditInfo.ActionDateTime = DateTime.Now;
            auditInfo.AuditInfoActionTypeId = (long)auditInfoActionTypeEnum;
            auditInfo.EntityId = (long)(NinjaSoftware.Enio.CoolJ.Enums.EntityEnum)Enum.Parse(typeof(NinjaSoftware.Enio.CoolJ.Enums.EntityEnum), entity.LLBLGenProEntityName.Replace("Entity", ""));
            auditInfo.UserId = this.UserId.Value;

            System.Collections.Generic.Dictionary<string, object> fieldsDictionary = new System.Collections.Generic.Dictionary<string, object>();

            foreach (IEntityField2 field in entity.Fields)
            {
                string concurrencyFieldName = ConfigurationManager.AppSettings["ConcurrencyFieldName"];
                // Saves all fields if is not update.
                // If it is update, saves only changed fileds.
                if (field.IsPrimaryKey)
                {
                    // primary key is saved in separately
                    auditInfo.PrimaryKeyValue = (long)field.CurrentValue;
                }
                else if (NinjaSoftware.Enio.CoolJ.Enums.AuditInfoActionTypeEnum.Delete == auditInfoActionTypeEnum)
                {
                    // nothing, you can recreate entity from other audit infos for this entity
                }
                else if ((auditInfoActionTypeEnum != NinjaSoftware.Enio.CoolJ.Enums.AuditInfoActionTypeEnum.Update || field.IsChanged) &&
                    !field.IsPrimaryKey &&
                    field.Name != concurrencyFieldName)
                {
                    fieldsDictionary.Add(field.Name, field.CurrentValue);
                }
            }

            auditInfo.JsonData = Newtonsoft.Json.JsonConvert.SerializeObject(fieldsDictionary);

            this.SaveEntity(auditInfo, false, false);
        }
		/// <summary>Initializes a new instance of the <see cref="DynamicRelation"/> class.</summary>
		/// <param name="leftOperand">The left operand, which is an entity.</param>
		/// <param name="joinType">Type of the join. If None is specified, Inner is assumed.</param>
		/// <param name="rightOperand">The right operand which is an entity.</param>
		/// <param name="aliasLeftOperand">The alias of the left operand. If you don't want to / need to alias the left operand (only alias if you have to), specify string.Empty.</param>
		/// <param name="aliasRightOperand">The alias of the right operand. If you don't want to / need to alias the right operand (only alias if you have to), specify string.Empty.</param>
		/// <param name="onClause">The on clause for the join.</param>
		public DynamicRelation(NinjaSoftware.Enio.CoolJ.EntityType leftOperand, JoinHint joinType, NinjaSoftware.Enio.CoolJ.EntityType rightOperand, string aliasLeftOperand, string aliasRightOperand, IPredicate onClause)
		{
			this.InitClass(joinType, aliasLeftOperand, aliasRightOperand, onClause, GeneralEntityFactory.Create(leftOperand), GeneralEntityFactory.Create(rightOperand));
		}
		/// <summary>General factory entrance method which will return an EntityFields2 object with the format generated by the factory specified</summary>
		/// <param name="relatedEntityType">The type of entity the fields are for</param>
		/// <returns>The IEntityFields instance requested</returns>
		public static IEntityFields2 CreateEntityFieldsObject(NinjaSoftware.Enio.CoolJ.EntityType relatedEntityType)
		{
			return FieldInfoProviderSingleton.GetInstance().GetEntityFields(InheritanceInfoProviderSingleton.GetInstance(), _entityTypeNamesCache[relatedEntityType]);
		}
		/// <summary>Initializes a new instance of the <see cref="DynamicRelation"/> class.</summary>
		/// <param name="leftOperand">The left operand.</param>
		/// <param name="joinType">Type of the join. If None is specified, Inner is assumed.</param>
		/// <param name="rightOperand">The right operand which is an entity type.</param>
		/// <param name="aliasRightOperand">The alias of the right operand. If you don't want to / need to alias the right operand (only alias if you have to), specify string.Empty.</param>
		/// <param name="onClause">The on clause for the join.</param>
		public DynamicRelation(DerivedTableDefinition leftOperand, JoinHint joinType, NinjaSoftware.Enio.CoolJ.EntityType rightOperand, string aliasRightOperand, IPredicate onClause)
		{
			this.InitClass(joinType, string.Empty, aliasRightOperand, onClause, leftOperand, GeneralEntityFactory.Create(rightOperand));
		}