private void UpdateIdOfAddedItem(SaveChangesResponse response)
 {
     if (this.Status == LoadingStatus.Added)
     {
         this.Model.Id = response.TryGetValue(DatabaseEntity.Patients.ToString());
     }
 }
Beispiel #2
0
        /// <summary>
        /// Save changes.
        /// </summary>
        /// <returns>Returns exception if any.</returns>
        public SaveChangesResponse Save()
        {
            var saveChangesResponse = new SaveChangesResponse();

            try
            {
                var watch = new Stopwatch();
                watch.Start();

                this.ReopenConnectionIfClosed();

                var addedEntities = this.entities.ObjectStateManager.GetObjectStateEntries(EntityState.Added).ToList();

                this.entities.SaveChanges();

                if (addedEntities.Any())
                {
                    this.HandleAddedEntities(addedEntities, saveChangesResponse);
                }

                watch.Stop();

                Log.Debug("Changes have been saved. Took {0}.", Log.Args(watch.Elapsed));

                return(saveChangesResponse);
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
                saveChangesResponse.SetException(ex);

                return(saveChangesResponse);
            }
        }
        private SaveChangesResponse CreateSaveChangesResponse(int id, bool isFailed)
        {
            SaveChangesResponse response;

            if (isFailed)
            {
                var excepton = this.fixture.Create <Exception>();
                response = new SaveChangesResponse(excepton);
            }
            else
            {
                response = new SaveChangesResponse();
                response.TryAddResult(DatabaseEntity.Educations.ToString(), id);
            }

            return(response);
        }
Beispiel #4
0
        private void HandleAddedEntities(IEnumerable <ObjectStateEntry> addedEntities, SaveChangesResponse response)
        {
            foreach (var entity in addedEntities)
            {
                var entityKey = entity.EntityKey;
                if (!entityKey.EntityKeyValues.Any())
                {
                    Log.Warn("No EntityKeyValues were found in object context.");
                    continue;
                }

                DatabaseEntity key;
                var            iskeyParsed = Enum.TryParse(entityKey.EntitySetName, out key);

                int value;
                var isValueParsed = int.TryParse(entityKey.EntityKeyValues[0].Value.ToString(), out value);

                if (iskeyParsed && isValueParsed)
                {
                    Log.Debug("Add value {0} with the key {1} to the save changes response.", Log.Args(value, key));
                    response.TryAddResult(key.ToString(), value);
                }
            }
        }