Пример #1
0
        public void Commit(ConcurrencyMode concurrencyMode = ConcurrencyMode.Throw)
        {
            #region Perform Auditing?

            //var entityAuditor = new EntityAuditor(IdentityInfo);

            //// Update Audit Information
            //foreach (var entity in ChangeTracker.Entries().Where(e => (e.State & (EntityState.Added | EntityState.Modified | EntityState.Deleted)) > 0))
            //{
            //    entityAuditor.AuditEntity(entity);
            //}

            #endregion

            CommitChanges(concurrencyMode);
            RaiseScheduledEvents();
            PublishPendingMessages();
        }
Пример #2
0
        /// <summary>
        /// Signal the DbContext to Save any Tracked Changes.
        /// </summary>
        /// <param name="concurrencyMode"></param>
        private void CommitChanges(ConcurrencyMode concurrencyMode)
        {
            bool saveFailed;

            do
            {
                saveFailed = false;

                try
                {
                    SaveChanges();
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    saveFailed = true;
                    var entry = ex.Entries.Single();

                    switch (concurrencyMode)
                    {
                    case ConcurrencyMode.ClientWins:
                        // Update original values from the database
                        entry.OriginalValues.SetValues(entry.GetDatabaseValues());
                        Log.Error(string.Format("Resolved concurrency error on {0} as \"Client Wins\".", entry.Entity.GetType().Name));
                        break;

                    case ConcurrencyMode.ServerWins:
                        // Update the values of the entity that failed to save from the store
                        ex.Entries.Single().Reload();
                        Log.Error(string.Format("Resolved concurrency error on {0} as \"Server Wins\".", entry.Entity.GetType().Name));
                        break;

                    case ConcurrencyMode.Throw:
                        Log.Error(string.Format("Unhandled concurrency error.  Type: {0}  State: {1}  Error: {2}", entry.Entity.GetType().Name, entry.State, ex));
                        throw;
                    }
                }
            } while (saveFailed);
        }