none
Update records in a table using Linq to Entities RRS feed

  • Question

  • Hi folks,

    Happy new year to all. Am working on a project but quite new to Linq To Entities.Using Entity Framework,  I want to update the "Individual" table in my entity object called "sheepGateEntities". The goal is:

    1. i want to update the table records  with any of the IndividualId values i choose but keeks on updating the first record of the table.

    2. I know it does this because of the use of First() method in my query expression.

    3. The question is  which Linq to Entities method do i use sothat my query expression can update based The specified IndividualId I choose?

    My code snippet is as seen below

    // Entity object

    SheepGateEntities sheepGateContext = new SheepGateEntities();

    var Indquery2 = sheepGateContext.Individual.Where(Ind => Ind.IndividualId == Ind.IndividualId).First();

    //modify individual information

                Indquery2.FirstName = TxtFName.Text;

                Indquery2.LastName = TxtLName.Text;

                Indquery2.StreetAddress = TxtAddress.Text;

                sheepGateContext.SaveChanges()

    //Save changes

    Nick

    Wednesday, January 2, 2013 7:55 PM

Answers

  • Hi Nick;

    Here is your issue. In your query you state in your Where clause to filter the result set such that only return records where the records Ind.IndividualId is equal to the same records Ind.IndividualId, which is always and so you return the first record of the collection because of the First method being applied. What you want to do is to return the record/s you want to modify. So for example lets say that you want to modify the record who's IndividualId is 132, then pass that into the query and if only one record has a IndividualId with that value then only one record will be returned if more then one record can have that value then you will get a collection of records who's IndividualId is equal to 132. The rest of your code is correct. using your code I have modified it to work assuming that IndividualId you are search for is 132, so modify as needed. Also please note that I have changed First to FirstOrDefault so that a null is returned if non is found so that it does not throw an exception.

    // Return the record/s who's IndividualId is 132
    int returnId = 132;
    
    // Entity object
    SheepGateEntities sheepGateContext = new SheepGateEntities();
    
    var Indquery2 = sheepGateContext.Individual.Where(Ind => Ind.IndividualId == returnId).FirstOrDefault();
    
    //modify individual information
    Indquery2.FirstName = TxtFName.Text;
    Indquery2.LastName = TxtLName.Text;
    Indquery2.StreetAddress = TxtAddress.Text;
    
    //Save changes
    sheepGateContext.SaveChanges()
    

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by fisco75 Wednesday, January 2, 2013 10:12 PM
    Wednesday, January 2, 2013 9:49 PM

All replies

  • Hi Nick;

    Here is your issue. In your query you state in your Where clause to filter the result set such that only return records where the records Ind.IndividualId is equal to the same records Ind.IndividualId, which is always and so you return the first record of the collection because of the First method being applied. What you want to do is to return the record/s you want to modify. So for example lets say that you want to modify the record who's IndividualId is 132, then pass that into the query and if only one record has a IndividualId with that value then only one record will be returned if more then one record can have that value then you will get a collection of records who's IndividualId is equal to 132. The rest of your code is correct. using your code I have modified it to work assuming that IndividualId you are search for is 132, so modify as needed. Also please note that I have changed First to FirstOrDefault so that a null is returned if non is found so that it does not throw an exception.

    // Return the record/s who's IndividualId is 132
    int returnId = 132;
    
    // Entity object
    SheepGateEntities sheepGateContext = new SheepGateEntities();
    
    var Indquery2 = sheepGateContext.Individual.Where(Ind => Ind.IndividualId == returnId).FirstOrDefault();
    
    //modify individual information
    Indquery2.FirstName = TxtFName.Text;
    Indquery2.LastName = TxtLName.Text;
    Indquery2.StreetAddress = TxtAddress.Text;
    
    //Save changes
    sheepGateContext.SaveChanges()
    

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by fisco75 Wednesday, January 2, 2013 10:12 PM
    Wednesday, January 2, 2013 9:49 PM
  • Hi Fernando,

    Thank you soo much for your reply. Thus the solution i  needed. At least i can smile once again. From your solution :

    int  returnId = 32;   was modified to

    int returnId = Convert.ToInt32(TxtId.Text);

    This allows me to enter any matching values in the database to be updated.  (TxtId is a textbox).

    Will contact you again in future when stranded.

    Cheers Friend


    Nick

    Wednesday, January 2, 2013 10:24 PM
  •   

    Not a problem, glad I was able to be of help.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, January 3, 2013 3:08 AM