update multiple records with entity framework 1

update multiple records with entity framework

using (var dbcontext = new MyModel()){
   var matchedRecords = dbcontext.DummyTable.Where(e => e.code.Equals(entry.code) && e.isValid.Equals(true)).ToList();
   matchedRecords.ForEach(e => e.isValid = false);
   dbcontext.SaveChanges();
}

Here is what the above code is Doing:
1. It is fetching all the records from the table where the code is equal to the code of the entry and isValid is true.
2. It is then setting the isValid to false for all the records fetched in step 1.
3. It is then saving the changes to the database.

Similar Posts