sadeq sadeq - 3 months ago 16
C# Question

How to use SaveChanges() for each row

I have table that has just 3 rows now I want update these rows for now I don't know how to save this row

if (_Shifts != null)
{
ContexManager contex = new ContexManager();
Shift _sht;
_Shifts[0].MyStartTime = dtStart1.Value.TimeOfDay;
_Shifts[0].MyEndTime = dtEnd1.Value.TimeOfDay;
_Shifts[0].MyName = txtShift1.Text;
contex.SaveChanges();
}


it not save changes now, how to do for save changes in database, it's for update row[0].

Answer

Run a loop over the values and finally add to a collection and do the SaveChanges()

if (_Shifts != null)
{
ContexManager contex = new ContexManager();
Foreach( var shiftitem in _Shifts)
{
  shiftitem.MyStartTime = dtStart1.Value.TimeOfDay;
  shiftitem.MyEndTime = dtEnd1.Value.TimeOfDay;
  shiftitem.MyName = txtShift1.Text;
  context.Shifts.Add(shiftitem);

}
 contex.SaveChanges();
}