none
strange behaviour updating table via linq, doesn't update views RRS feed

  • Question

  • I have come across a strange behaviour in linq to sql. I hope that it will be easy to solve as I have already spent to much time on it.

    I have a number of views which have multiple tables. I noticed when I was updating records via linq that the item being updated would be removed from the view (I update the underlying table and then on another form refresh the views data to re-display in a datagridview which is not databound). The data is still in the underlying table and fine, it simply disappears from the view (when running the view in SQLServer management studio and in my application). When running a basic update query in SQL to the underlying tables, the items are still in the view, they do not disappear. I also tried two different techniques via linq to SQL, the first one being:

    var getSeed = (from gs in treeNurseryDatabase.seeds
                                       where gs.seedID == seedID
                                       select gs).SingleOrDefault();
                        if (getSeed != null)
                        {
                            getSeed.speciesID = inSeed.speciesID;
                            getSeed.seedOriginCode = inSeed.seedOriginCode;
                            getSeed.dateCollected = inSeed.dateCollected;
                            getSeed.seedDetails = inSeed.seedDetails;
                            getSeed.dateReceived = inSeed.dateReceived;
                            if (inSeed.seedLotNumber != "")
                            {
                                getSeed.seedLotNumber = inSeed.seedLotNumber;
                            }

                            treeNurseryDatabase.SubmitChanges();

                            return "";

    The code above results in the strange behaviour.

    The second linq code I tried was the following:

    var getSeed = (from gs in treeNurseryDatabase.seeds
                                       where gs.seedID == 467476
                                       select gs).SingleOrDefault();
                        if (getSeed != null)
                        {
                            getSeed.seedOriginCode = "something different";

                            treeNurseryDatabase.SubmitChanges();

                            return "";

    The code above does not remove the item from the view. I have tried rebooting and waiting a while and the items still will not appear in the view. The update in all cases does update the underlying tables before they disappear from the view. 

    Thanks in advance if anyone has any knowledge on this issue!

    Let me know if you need more information,

    Kind regards,


    Rebecca

    Monday, November 16, 2009 6:34 AM

Answers

  • Is it possible that you are changing the item such that it no longer matches the view definition? Perhaps your view has a WHERE clause that includes one of the properties you change in the first cod example, but not in the second.
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    Monday, November 16, 2009 1:43 PM
    Answerer

All replies

  • Is it possible that you are changing the item such that it no longer matches the view definition? Perhaps your view has a WHERE clause that includes one of the properties you change in the first cod example, but not in the second.
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    Monday, November 16, 2009 1:43 PM
    Answerer
  • Thank you for the reply! 

    I found the problem within 10 minutes this morning! I think it pays to sleep on things sometimes, after 10 hours of programming the mind probably isn't quite as sharp as usual heh.

    The problem was a commented out line of code

    //newSeed.speciesID = speciesDictionary[cmbSpeciesCode.Text.Trim()].speciesID;

    In the update statement it was setting "getSeed.speciesID = inSeed.speciesID;" but of course i had commented out the code before calling the update so it was passing null ontop of the foreign key, therefore breaking it.

    Well I'm glad it was an easy fix, thanks again for your help. Next time I will sleep on it before posting.

    Regards,


    Bex
    Monday, November 16, 2009 8:35 PM