Inheritance: OrderStatusBase
示例#1
0
        /// <summary>
        ///     Inserts a PetShop.Business.OrderStatus object into the datasource using a transaction.
        /// </summary>
        /// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
        /// <param name="entity">PetShop.Business.OrderStatus object to insert.</param>
        /// <remarks>
        ///		After inserting into the datasource, the PetShop.Business.OrderStatus object will be updated
        ///     to refelect any changes made by the datasource. (ie: identity or computed columns)
        /// </remarks>
        /// <returns>Returns true if operation is successful.</returns>
        /// <exception cref="System.Exception">The command could not be executed.</exception>
        /// <exception cref="System.Data.DataException">The <paramref name="transactionManager"/> is not open.</exception>
        /// <exception cref="System.Data.Common.DbException">The command could not be executed.</exception>
        public override bool Insert(TransactionManager transactionManager, PetShop.Business.OrderStatus entity)
        {
            SqlDatabase database       = new SqlDatabase(this._connectionString);
            DbCommand   commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "dbo.OrderStatus_Insert", _useStoredProcedure);

            database.AddInParameter(commandWrapper, "@OrderId", DbType.Int32, entity.OrderId);
            database.AddInParameter(commandWrapper, "@LineNum", DbType.Int32, entity.LineNum);
            database.AddInParameter(commandWrapper, "@Timestamp", DbType.DateTime, entity.Timestamp);
            database.AddInParameter(commandWrapper, "@Status", DbType.AnsiString, entity.Status);

            int results = 0;

            //Provider Data Requesting Command Event
            OnDataRequesting(new CommandEventArgs(commandWrapper, "Insert", entity));

            if (transactionManager != null)
            {
                results = Utility.ExecuteNonQuery(transactionManager, commandWrapper);
            }
            else
            {
                results = Utility.ExecuteNonQuery(database, commandWrapper);
            }


            entity.OriginalOrderId = entity.OrderId;
            entity.OriginalLineNum = entity.LineNum;

            entity.AcceptChanges();

            //Provider Data Requested Command Event
            OnDataRequested(new CommandEventArgs(commandWrapper, "Insert", entity));

            return(Convert.ToBoolean(results));
        }
        protected override void DataPortal_Update()
        {
            bool cancel = false;
            OnUpdating(ref cancel);
            if (cancel) return;

            if(OriginalOrderId != OrderId || OriginalLineNum != LineNum)
            {
                // Insert new child.
                OrderStatus item = new OrderStatus {OrderId = OrderId, LineNum = LineNum, Timestamp = Timestamp, Status = Status};
                
                item = item.Save();

                // Mark editable child lists as dirty. This code may need to be updated to one-to-one relationships.

                // Create a new connection.
                using (var connection = new SqlConnection(ADOHelper.ConnectionString))
                {
                    connection.Open();
                    FieldManager.UpdateChildren(this, connection);
                }

                // Delete the old.
                var criteria = new OrderStatusCriteria {OrderId = OriginalOrderId, LineNum = OriginalLineNum};
                
                DataPortal_Delete(criteria);

                // Mark the original as the new one.
                OriginalOrderId = OrderId;
                OriginalLineNum = LineNum;

                OnUpdated();

                return;
            }

            const string commandText = "UPDATE [dbo].[OrderStatus] SET [OrderId] = @p_OrderId, [LineNum] = @p_LineNum, [Timestamp] = @p_Timestamp, [Status] = @p_Status WHERE [OrderId] = @p_OriginalOrderId AND [LineNum] = @p_OriginalLineNum; SELECT [OrderId], [LineNum] FROM [dbo].[OrderStatus] WHERE [OrderId] = @p_OriginalOrderId AND [LineNum] = @p_OriginalLineNum";
            using (var connection = new SqlConnection(ADOHelper.ConnectionString))
            {
                connection.Open();
                using(var command = new SqlCommand(commandText, connection))
                {
                    command.Parameters.AddWithValue("@p_OriginalOrderId", this.OriginalOrderId);
                command.Parameters.AddWithValue("@p_OrderId", this.OrderId);
                command.Parameters.AddWithValue("@p_OriginalLineNum", this.OriginalLineNum);
                command.Parameters.AddWithValue("@p_LineNum", this.LineNum);
                command.Parameters.AddWithValue("@p_Timestamp", this.Timestamp);
                command.Parameters.AddWithValue("@p_Status", this.Status);

                    //result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. 
                    int result = command.ExecuteNonQuery();
                    if (result == 0)
                        throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute.");

                    LoadProperty(_originalOrderIdProperty, this.OrderId);
                    LoadProperty(_originalLineNumProperty, this.LineNum);
                }

                FieldManager.UpdateChildren(this, connection);
            }

            OnUpdated();
        }