public void SaveItems(Order order)
        {
            //TODO: I know this need to be rewritting
            //the problem I have here is that I need it ALL to be in the scope of a single DB transaction

            //Ayende <3 this method :p

            using (DB db = new DB()) {
                //see if there is an order in the DB already
                Commerce.Data.SqlRepository.Order
                    existingorder = (from o in db.Orders
                                     where o.OrderID == order.ID
                                     select o).SingleOrDefault();

                //if not, create it
                if (existingorder == null)
                {
                    existingorder                  = new Commerce.Data.SqlRepository.Order();
                    existingorder.UserName         = order.UserName;
                    existingorder.CreatedOn        = DateTime.Now;
                    existingorder.ModifiedOn       = DateTime.Now;
                    existingorder.OrderID          = order.ID;
                    existingorder.OrderStatusID    = (int)order.Status;
                    existingorder.UserLanguageCode = System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
                    db.Orders.InsertOnSubmit(existingorder);
                }
                else
                {
                    //there's a order - pull all the ProductIDs from our Model
                    var productsInBasket = from p in order.Items

                                           select p.Product.ID;

                    //first, drop the items in the DB that aren't in the order
                    var deletedProducts = from si in db.OrderItems
                                          where !productsInBasket.Contains(si.ProductID) &&
                                          si.OrderID == order.ID
                                          select si;

                    db.OrderItems.DeleteAllOnSubmit(deletedProducts);


                    //update the ones that have changed - this applies to Quantity
                    foreach (Commerce.Data.SqlRepository.OrderItem dbItem in existingorder.OrderItems)
                    {
                        OrderItem orderItem = order.Items.Where(
                            x => x.OrderID == dbItem.OrderID &&
                            x.Product.ID == dbItem.ProductID
                            ).SingleOrDefault();

                        //if the quantity has changed, update it
                        if (orderItem != null && dbItem.Quantity != orderItem.Quantity)
                        {
                            dbItem.Quantity = orderItem.Quantity;
                        }
                    }
                }


                //finally, add the items that are new (ID==0)
                //setup the items to load up
                foreach (OrderItem newItem in order.Items)
                {
                    //see if the product is in the existing order
                    Commerce.Data.SqlRepository.OrderItem existingItem = (from items in existingorder.OrderItems
                                                                          where items.ProductID == newItem.Product.ID
                                                                          select items).SingleOrDefault();

                    if (existingItem == null)
                    {
                        existingItem               = new Commerce.Data.SqlRepository.OrderItem();
                        existingItem.DateAdded     = DateTime.Now;
                        existingItem.OrderID       = existingorder.OrderID;
                        existingItem.ProductID     = newItem.Product.ID;
                        existingItem.LineItemPrice = newItem.LineItemPrice;
                    }

                    existingItem.Quantity = newItem.Quantity;
                    existingorder.OrderItems.Add(existingItem);
                }



                if (order.ShippingAddress != null)
                {
                    existingorder.ShippingAddressID = order.ShippingAddress.ID;
                }


                //save it in a batch - this is a transaction
                db.SubmitChanges();
            }
        }
        public void SaveOrder(Order order)
        {
            //save down the addresses
            if (order.ShippingAddress != null)
            {
                SaveAddress(order.ShippingAddress);
            }

            if (order.BillingAddress != null)
            {
                SaveAddress(order.BillingAddress);
            }


            using (DB db = new DB())
            {
                //pull the order
                Commerce.Data.SqlRepository.Order existingOrder = (from o in db.Orders
                                                                   where o.OrderID == order.ID
                                                                   select o).SingleOrDefault();
                if (existingOrder == null)
                {
                    throw new InvalidOperationException("There is no order with the ID " + order.ID.ToString());
                }

                //marry up the orders
                existingOrder.TaxAmount = order.TaxAmount;

                if (order.ShippingMethod != null)
                {
                    existingOrder.ShippingAmount   = order.ShippingMethod.Cost;
                    existingOrder.ShippingMethodID = order.ShippingMethod.ID;

                    //shipping method bits
                    existingOrder.EstimatedDelivery = DateTime.Now.AddDays(order.ShippingMethod.DaysToDeliver);
                }
                existingOrder.SubTotal      = order.SubTotal;
                existingOrder.OrderStatusID = (int)order.Status;

                if (order.ShippingAddress != null)
                {
                    existingOrder.ShippingAddressID = order.ShippingAddress.ID;
                }

                if (order.BillingAddress != null)
                {
                    existingOrder.BillingAddressID = order.BillingAddress.ID;
                }

                existingOrder.ExecutedOn     = DateTime.Now;
                existingOrder.OrderNumber    = order.OrderNumber;
                existingOrder.UserName       = order.UserName;
                existingOrder.DiscountAmount = order.DiscountAmount;
                existingOrder.DiscountReason = order.DiscountReason;

                //save this down so we know how to correspond in the future
                existingOrder.UserLanguageCode = System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;

                foreach (Transaction t in order.Transactions)
                {
                    Commerce.Data.SqlRepository.Transactions newTransaction = new Commerce.Data.SqlRepository.Transactions();

                    //a little left/right action...
                    newTransaction.TransactionID     = t.ID;
                    newTransaction.OrderID           = t.OrderID;
                    newTransaction.Notes             = t.Notes;
                    newTransaction.AuthorizationCode = t.AuthorizationCode;
                    newTransaction.Amount            = t.Amount;
                    newTransaction.ProcessorID       = (int)t.Processor;
                    newTransaction.TransactionDate   = t.DateExecuted;

                    db.Transactions.InsertOnSubmit(newTransaction);
                }

                //cross your fingers!
                db.SubmitChanges();
            }
        }
		private void detach_Orders(Order entity)
		{
			this.SendPropertyChanging();
			entity.ShippingMethod = null;
		}
        public void SaveItems(Order order) {
            //TODO: I know this need to be rewritting
            //the problem I have here is that I need it ALL to be in the scope of a single DB transaction

            //Ayende <3 this method :p

            using (DB db = new DB()) {

                //see if there is an order in the DB already
                Commerce.Data.SqlRepository.Order
                    existingorder = (from o in db.Orders
                                    where o.OrderID==order.ID
                                    select o).SingleOrDefault();

                //if not, create it
                if (existingorder == null) {

                    existingorder = new Commerce.Data.SqlRepository.Order();
                    existingorder.UserName = order.UserName;
                    existingorder.CreatedOn = DateTime.Now;
                    existingorder.ModifiedOn = DateTime.Now;
                    existingorder.OrderID = order.ID;
                    existingorder.OrderStatusID = (int) order.Status;
                    existingorder.UserLanguageCode = System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
                    db.Orders.InsertOnSubmit(existingorder);

                } else {

                    //there's a order - pull all the ProductIDs from our Model
                    var productsInBasket = from p in order.Items
                                  
                                           select p.Product.ID;

                    //first, drop the items in the DB that aren't in the order
                    var deletedProducts = from si in db.OrderItems
                               where !productsInBasket.Contains(si.ProductID) 
                               && si.OrderID == order.ID
                               select si;

                    db.OrderItems.DeleteAllOnSubmit(deletedProducts);
                           

                    //update the ones that have changed - this applies to Quantity
                    foreach (Commerce.Data.SqlRepository.OrderItem dbItem in existingorder.OrderItems) {

                        OrderItem orderItem = order.Items.Where(
                                x => x.OrderID == dbItem.OrderID 
                                && x.Product.ID == dbItem.ProductID
                                ).SingleOrDefault();

                        //if the quantity has changed, update it
                        if (orderItem != null && dbItem.Quantity != orderItem.Quantity) {
                            dbItem.Quantity = orderItem.Quantity;
                        }

                    }

                }


                //finally, add the items that are new (ID==0)
                //setup the items to load up
                foreach (OrderItem newItem in order.Items) {

                    //see if the product is in the existing order
                    Commerce.Data.SqlRepository.OrderItem existingItem = (from items in existingorder.OrderItems
                                                                                     where items.ProductID == newItem.Product.ID
                                                                                     select items).SingleOrDefault();

                    if (existingItem == null) {
                        existingItem = new Commerce.Data.SqlRepository.OrderItem();
                        existingItem.DateAdded = DateTime.Now;
                        existingItem.OrderID = existingorder.OrderID;
                        existingItem.ProductID = newItem.Product.ID;
                        existingItem.LineItemPrice = newItem.LineItemPrice; 
                       
                    }

                    existingItem.Quantity = newItem.Quantity;
                    existingorder.OrderItems.Add(existingItem);
                }



                if (order.ShippingAddress != null)
                    existingorder.ShippingAddressID = order.ShippingAddress.ID;


                //save it in a batch - this is a transaction
                db.SubmitChanges();

            }


        }
		private void attach_Orders1(Order entity)
		{
			this.SendPropertyChanging();
			entity.Address1 = this;
		}
		private void detach_Orders1(Order entity)
		{
			this.SendPropertyChanging();
			entity.Address1 = null;
		}
		private void detach_Orders(Order entity)
		{
			this.SendPropertyChanging();
			entity.OrderStatus = null;
		}
		private void attach_Orders(Order entity)
		{
			this.SendPropertyChanging();
			entity.OrderStatus = this;
		}
 partial void DeleteOrder(Order instance);
 partial void UpdateOrder(Order instance);
 partial void InsertOrder(Order instance);