コード例 #1
0
            private static RevertTable Create(string tableName, string primaryKeyValue, DatabaseTable databaseTable, int index)
            {
                //Used to create a new Revert table record.  Will instanciate the Primary Key change
                RevertTable table = new RevertTable {
                    TableName       = tableName,
                    PrimaryKeyValue = primaryKeyValue,
                    DatabaseTable   = databaseTable,
                    Index           = index,
                    Fields          = new List <RevertField>()
                };
                //Get the Primary Key
                var primaryKeyColumn = databaseTable.Fields.FirstOrDefault(x => x.IsPrimaryKey);

                if (primaryKeyColumn != null)
                {
                    //Set the Primary Key Value
                    table.Fields.Add(new RevertField {
                        FieldName     = primaryKeyColumn.ColumnName,
                        DatabaseField = primaryKeyColumn,
                        FieldValue    = primaryKeyValue
                    });
                }
                else
                {
                    throw new NotSupportedException("Cannot find Primary Key for this table");
                }

                return(table);
            }
コード例 #2
0
        private void RollbackChanges()
        {
            using (var db = _getDb()) {
                //using (var trans = new TransactionScope()) {

                //}

                //Get the rows that we need to work on
                Audit[] submissions = db.Audits.Include(x => x.Details).Where(a => a.Id > _auditCheckpoint).OrderByDescending(g => g.Id).ToArray();

                //Get the collection of database tables
                var databaseTables = GetDatabaseTables(db);
                //Create an empty collection of what we need to rollback
                List <RevertTable> rollbackRecords = new List <RevertTable>();

                //Loop through all of the transactions that occured, and apply them so we have rollback records
                foreach (var submission in submissions)
                {
                    RevertTable.ApplyTransaction(_runType == RunTypes.Simple, rollbackRecords, databaseTables, submission);
                }

                //Rollback our changes
                foreach (RevertTable rollbackRecord in rollbackRecords.OrderBy(x => x.Index))
                {
                    //Build the statement to revert this entity
                    SqlStatement statement = rollbackRecord.BuildSqlStatement();
                    System.Diagnostics.Debug.WriteLine(statement.Sql);
                    //Execute the statement to revert this entity
                    db.Database.ExecuteSqlCommand(statement.Sql, statement.SqlParameters.ToArray());
                }

                //Remove the Audit  Detail Rows that were just updated.
                db.AuditDetails.RemoveRange(db.AuditDetails.Where(a => a.AuditId > _auditCheckpoint));
                //Remove the Audit  Rows that were just updated.
                db.Audits.RemoveRange(db.Audits.Where(a => a.Id > _auditCheckpoint));
                db.SaveChanges();
            }
        }