// Constructors

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        protected Entity()
        {
            try {
                var key = Key.Generate(Session, GetType());
                State = Session.CreateEntityState(key, true);
                changeVersionOnSetAttempt = ShouldChangeOnSetAttempt();
                SystemBeforeInitialize(false);
            }
            catch (Exception error) {
                InitializationError(GetType(), error);
                // GetType() call is correct here: no code will be executed further,
                // if base constructor will fail, but since descendant's constructor is aspected,
                // we must "simulate" its own call of InitializationError method.
                throw;
            }
        }
 // Is used for EntitySetItem<,> instance construction
 internal Entity(Session session, Tuple keyTuple)
     : base(session)
 {
     try {
         ArgumentValidator.EnsureArgumentNotNull(keyTuple, "keyTuple");
         var key = Key.Create(Session.Domain, Session.StorageNodeId, GetTypeInfo(), TypeReferenceAccuracy.ExactType, keyTuple);
         State = Session.CreateEntityState(key, true);
         changeVersionOnSetAttempt = ShouldChangeOnSetAttempt();
         SystemBeforeInitialize(false);
         Initialize(GetType());
     }
     catch (Exception error) {
         InitializationError(GetType(), error);
         throw;
     }
 }
 /// <summary>
 ///   Initializes a new instance of this class.
 /// </summary>
 /// <param name="session">The session.</param>
 /// <param name="values">The field values that will be used for key building.</param>
 /// <remarks>Use this kind of constructor when you need to explicitly set key for this instance.</remarks>
 /// <example>
 ///     <code>
 /// [HierarchyRoot]
 /// public class Book : Entity
 /// {
 /// [Field, KeyField]
 /// public string ISBN { get; set; }
 /// public Book(string isbn) : base(isbn) { }
 /// }
 /// </code>
 /// </example>
 protected Entity(Session session, params object[] values)
     : base(session)
 {
     try {
         ArgumentValidator.EnsureArgumentNotNull(values, "values");
         var key = Key.Create(Session.Domain, Session.StorageNodeId, GetTypeInfo(), TypeReferenceAccuracy.ExactType, values);
         State = Session.CreateEntityState(key, true);
         changeVersionOnSetAttempt = ShouldChangeOnSetAttempt();
         RegisterKeyFieldsOfEntityTypeForRemap(key, values);
         var operations = Session.Operations;
         using (operations.BeginRegistration(OperationType.System)) {
             if (operations.CanRegisterOperation)
             {
                 operations.RegisterOperation(new EntityInitializeOperation(key), true);
             }
             var references = TypeInfo.Key.Fields.Where(f => f.IsEntity && f.Associations.Any(a => a.IsPaired)).ToList();
             if (references.Count > 0)
             {
                 using (Session.DisableSaveChanges(this)) {
                     foreach (var referenceField in references)
                     {
                         var referenceValue = (Entity)GetFieldValue(referenceField);
                         Session.PairSyncManager.ProcessRecursively(null, null,
                                                                    PairIntegrity.OperationType.Set, referenceField.GetAssociation(referenceValue.TypeInfo), this, referenceValue, null);
                     }
                 }
             }
         }
         SystemBeforeInitialize(false);
     }
     catch (Exception error) {
         InitializationError(GetType(), error);
         // GetType() call is correct here: no code will be executed further,
         // if base constructor will fail, but since descendant's constructor is aspected,
         // we must "simulate" its own call of InitializationError method.
         throw;
     }
 }