示例#1
0
        public virtual void Commit(Transaction transaction)
        {
            #region Check Prerequisites

            if (transaction == null)
            {
                throw new ArgumentNullException("transaction");
            }
            if (string.IsNullOrWhiteSpace(transaction.TransactionType))
            {
                throw new ArgumentException("transactionInfo.TipoTransaccion");
            }
            if (string.IsNullOrWhiteSpace(transaction.ModifiedBy))
            {
                throw new ArgumentException("transactionInfo.ModificadoPor");
            }

            #endregion Check Prerequisites

            ObjectContext objectContext = ((IObjectContextAdapter)this).ObjectContext;
            try
            {
                objectContext.Connection.Open();

                // Resetenado el detalla de las transacciones.
                transaction.TransactionDetail = new List <TransactionDetail>();

                using (var scope = TransactionScopeFactory.GetTransactionScope())
                {
                    var changedEntities = new List <ModifiedEntityEntry>();
                    var tableMapping    = new List <EntityMapping>();
                    var sqlCommandInfos = new List <SqlCommandInfo>();

                    // Get ebtities with changes.
                    IEnumerable <DbEntityEntry> changedDbEntityEntries = GetChangedDbEntityEntries();

                    // Apply transaction info.
                    foreach (DbEntityEntry entry in changedDbEntityEntries)
                    {
                        ApplyTransactionInfo(transaction, entry);

                        // Get the deleted records info first
                        if (entry.State == EntityState.Deleted)
                        {
                            EntityMapping  entityMapping  = GetEntityMappingConfiguration(tableMapping, entry);
                            SqlCommandInfo sqlCommandInfo = GetSqlCommandInfo(transaction, entry, entityMapping);
                            if (sqlCommandInfo != null)
                            {
                                sqlCommandInfos.Add(sqlCommandInfo);
                            }

                            transaction.AddDetail(entityMapping.TableName, entry.State.ToString(), transaction.TransactionType);
                        }
                        else
                        {
                            changedEntities.Add(new ModifiedEntityEntry(entry, entry.State.ToString()));
                        }
                    }

                    base.SaveChanges();

                    // Get the Added and Mdified records after changes, that way we will be able to get the generated .
                    foreach (ModifiedEntityEntry entry in changedEntities)
                    {
                        EntityMapping  entityMapping  = GetEntityMappingConfiguration(tableMapping, entry.EntityEntry);
                        SqlCommandInfo sqlCommandInfo = GetSqlCommandInfo(transaction, entry.EntityEntry, entityMapping);
                        if (sqlCommandInfo != null)
                        {
                            sqlCommandInfos.Add(sqlCommandInfo);
                        }

                        transaction.AddDetail(entityMapping.TableName, entry.State, transaction.TransactionType);
                    }

                    // Adding Audit Detail Transaction CommandInfo.
                    sqlCommandInfos.AddRange(GetAuditRecords(transaction));

                    // Insert Transaction and audit records.
                    foreach (SqlCommandInfo sqlCommandInfo in sqlCommandInfos)
                    {
                        Database.ExecuteSqlCommand(sqlCommandInfo.Sql, sqlCommandInfo.Parameters);
                    }

                    scope.Complete();
                }
            }
            finally
            {
                objectContext.Connection.Close();
            }
        }
示例#2
0
        private SqlCommandInfo GetSqlCommandInfo(Transaction transaction, DbEntityEntry entry, EntityMapping entityMapping)
        {
            if (entityMapping.TableName.Contains("_Transacciones"))
            {
                return(null);
            }

            string sqlInsert;

            object[] param;
            CreateTransactionInsertStatement(entityMapping, entry, transaction, out sqlInsert, out param);

            var sqlCommandInfo = new SqlCommandInfo(sqlInsert, param);

            return(sqlCommandInfo);
        }