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 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 detach_OrderItems(OrderItem entity)
		{
			this.SendPropertyChanging();
			entity.Order = null;
		}
		private void attach_OrderItems(OrderItem entity)
		{
			this.SendPropertyChanging();
			entity.Order = this;
		}
 partial void DeleteOrderItem(OrderItem instance);
 partial void UpdateOrderItem(OrderItem instance);
 partial void InsertOrderItem(OrderItem instance);