none
LINQ saving unchanged values RRS feed

  • Question

  • I'm having issues wrapping my head around an issue and any help would be appreciated. I have an edit screen for products. With various hidden and input fields I have I have all the properties of the product in a form that posts to an update method. I also have an image associated with the product and form. My update code looks like this...

            public ActionResult Update(Product product, HttpPostedFileBase image)
            {
                if (image != null)
                {
                    product.ThumbImageMimeType = image.ContentType;
                    product.ThumbImageData = new byte[image.ContentLength];
                    image.InputStream.Read(product.ThumbImageData, 0, image.ContentLength);
                }
                productsRepository.SaveProduct(product);
                TempData["AdminMessage"] = "Product Changes Saved";
                return RedirectToAction("List");
            }

    The issue I'm having is if the user updates product details and does not update the image, my LINQ query is saving the NULL values from the form. Any ideas on how to solve this. I'm not sure if I can exclude certain fields from being updated or not with LINQ.

    Also, here's my Save Products Method...

            public void SaveProduct(Product product)
            {
                if (product.ProductID == null)
                    productsTable.InsertOnSubmit(product);
                else
                {
                    productsTable.Attach(product);
                    productsTable.Context.Refresh(RefreshMode.KeepCurrentValues, product);
                }

                productsTable.Context.SubmitChanges();
            }

    Thanks in advance!
    Friday, September 18, 2009 5:49 AM

Answers

  • Hi Dave,

     

    When you are using attach for updating, please pay attention to the order of your code.

    For example,

    If you want to modify the Stu with id = 99 and name = “Edith”, you write this,

                Stu s = new Stu() { id = 99};

                s.name = "Monica";

                context.Attach ("Stus", s);

     

    Nothing would be changed when you call SubmitChanges because no record is the id of 99 and name of Monica. The attach is failed actually.

    However, if you attach just with the id field, the updating will work.

                Stu s = new Stu() { id = 99};

                context.AttachTo("Stus", s);

                s.name = "Monica";

     

    Thus, you need to attach the object with id first then set the value you want to update.

    For more information, you can refer to this blog:

    http://www.richardbushnell.net/index.php/2008/02/18/how-to-update-data-with-linq-to-sql/

     

     

    Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Yichun_Feng Friday, September 25, 2009 1:51 AM
    Monday, September 21, 2009 8:59 AM