public override async Task <int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
        {
            int result;
            var timestamp = _systemClock.UtcNow;

            IDbContextTransaction transaction = null;

            if (Database.CurrentTransaction == null && Database.GetEnlistedTransaction() == null && Transaction.Current == null)
            {
                transaction = await Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
            }

            using (transaction)
            {
                result = await base.SaveChangesAsync(false, cancellationToken).ConfigureAwait(false);

                var changes = ChangeTracker.Entries()
                              .Where(_ => _.ShouldAuditChanges())
                              .Select(entry => CreateChangeEvent(timestamp, entry))
                              .ToList();

                ChangeTracker.AcceptAllChanges();

                await ChangeEvents.AddRangeAsync(changes, cancellationToken).ConfigureAwait(false);

                result += await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken).ConfigureAwait(false);

                transaction?.Commit();
            }

            return(result);
        }
        public override int SaveChanges(bool acceptAllChangesOnSuccess)
        {
            int rowsAffected;
            var timestamp = _systemClock.UtcNow;

            IDbContextTransaction transaction = null;

            if (Database.CurrentTransaction == null && Database.GetEnlistedTransaction() == null && Transaction.Current == null)
            {
                transaction = Database.BeginTransaction();
            }

            using (transaction)
            {
                rowsAffected = base.SaveChanges(false);

                var changes = ChangeTracker.Entries()
                              .Where(_ => _.ShouldAuditChanges())
                              .Select(entry => CreateChangeEvent(timestamp, entry))
                              .ToList();

                ChangeTracker.AcceptAllChanges();

                ChangeEvents.AddRange(changes);

                rowsAffected += base.SaveChanges(acceptAllChangesOnSuccess);

                transaction?.Commit();
            }

            return(rowsAffected);
        }
        public override async Task <int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
        {
            ChangeTracker.DetectChanges();

            var markedAsDeleted = ChangeTracker.Entries().Where(x => x.State == EntityState.Deleted).ToList();

            foreach (var item in markedAsDeleted)
            {
                if (item.Entity is BaseModel entity)
                {
                    // Set the entity to unchanged (if we mark the whole entity as Modified, every field gets sent to Db as an update)
                    item.State = EntityState.Unchanged;

                    entity.DeletedAt = DateTime.Now;
                }
            }

            var result = await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);

            foreach (var item in markedAsDeleted)
            {
                item.State = EntityState.Deleted;
            }

            ChangeTracker.AcceptAllChanges();

            return(result);
        }
Exemple #4
0
        private int OnSaveChanges2()
        {
            ChangeTracker.DetectChanges();

            var modified = ChangeTracker.Entries()
                           .Where(x => x.State == EntityState.Modified && x.Entity != null)
                           .ToList();
            var added = ChangeTracker.Entries()
                        .Where(x => x.State == EntityState.Added && x.Entity != null)
                        .ToList();
            var deleted = ChangeTracker.Entries()
                          .Where(x => x.State == EntityState.Deleted && x.Entity != null)
                          .ToList();

            var connection = Database.GetDbConnection();

            if (connection.State == System.Data.ConnectionState.Closed)
            {
                connection.Open();
            }
            if (Cnn.ExternalTransaction != null)
            {
                UpdateObjectStateEntries(modified, (content, item) => item.Properties.Where(x => x.IsModified).Select(x => x.Metadata.Name).ToArray(), true);
                UpdateObjectStateEntries(added, (content, item) => GetProperties(content), false);

                foreach (var deletedItem in deleted)
                {
                    var article = (IQPArticle)deletedItem.Entity;
                    Cnn.DeleteContentItem(article.Id);
                }
            }
            else
            {
                using (var transaction = Database.BeginTransaction())
                {
                    Cnn.ExternalTransaction = transaction.GetDbTransaction();

                    UpdateObjectStateEntries(modified, (content, item) => item.Properties.Where(x => x.IsModified).Select(x => x.Metadata.Name).ToArray(), true);
                    UpdateObjectStateEntries(added, (content, item) => GetProperties(content), false);

                    foreach (var deletedItem in deleted)
                    {
                        var article = (IQPArticle)deletedItem.Entity;
                        Cnn.DeleteContentItem(article.Id);
                    }

                    transaction.Commit();
                    Cnn.ExternalTransaction = null;
                }
            }

            //connection.Close();
            ChangeTracker.AcceptAllChanges();
            return(0);
        }
Exemple #5
0
        public override async Task <int> SaveChangesAsync(CancellationToken cancellationToken = default)
        {
            using (var transaction = await Database.BeginTransactionAsync(cancellationToken))
            {
                SetAuditTimestamps();
                var result = await base.SaveChangesAsync(acceptAllChangesOnSuccess : false, cancellationToken);
                await ExecuteExternalOperations();

                ChangeTracker.AcceptAllChanges();
                await transaction.CommitAsync(cancellationToken);

                return(result);
            }
        }
        public override int SaveChanges()
        {
            if (!_persistOnSave)
            {
                return(0);
            }

            var result = base.SaveChanges(false);

            var auditLogs = GetAuditLogs().ToList();

            ChangeTracker.AcceptAllChanges();

            AuditLogs.AddRange(auditLogs);
            base.SaveChanges();

            return(result);
        }
Exemple #7
0
 /// <summary>
 /// 接受所有更改
 /// </summary>
 public virtual void AcceptAllChanges()
 {
     ChangeTracker.AcceptAllChanges();
 }