private void detach_Orders(Order entity)
			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
                    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;

                } 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;


                    //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

                        //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;

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

                //save it in a batch - this is a transaction


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