I do not quite understand why this is happening. When I add a new entry on the template and save the new values ​​everything is fine. Added to the database and updated grid. And when I try to update the values ​​of the existing line in the database, the updates go through, but the grid contains old values. Am I working incorrectly with the context? I give the code.

There is a form1

public partial class IncidentJournal : Form { VGSOContext db; public IncidentJournal() { InitializeComponent(); db = new VGSOContext(); db.IncidentJournal.Load(); db.CorrectiveAction.Load(); dateEdit1.EditValue = DateTime.Now; } private void LoadMainGrid(int y, int m) { gridMainGrid.DataSource = db.IncidentJournal.Where(p => p.IncidentDatetime.Year == y && p.IncidentDatetime.Month == m).ToList(); } private void IncidentJournal_Load(object sender, EventArgs e) { int y = dateEdit1.DateTime.Year; int m = dateEdit1.DateTime.Month; LoadMainGrid(y, m); } private object ShowSomething(object r) { EditIncidentJournal f2 = new EditIncidentJournal(r); f2.ShowDialog(); return f2.SomeData; } private void SBAddIncJournal_Click(object sender, EventArgs e) { int id = Convert.ToInt32(gridViewMain.GetRowCellValue(gridViewMain.FocusedRowHandle, "Id").ToString()); using (var t = new VGSOContext()) { var i = t.IncidentJournal.Where(x => x.Id == id).Single(); object j = ShowSomething(i); i = (VGSO.Models.IncidentJournal)j; t.IncidentJournal.Add(i); t.SaveChanges(); } LoadMainGrid(dateEdit1.DateTime.Year, dateEdit1.DateTime.Month); } private void SBEditIncJournal_Click(object sender, EventArgs e) { int id = Convert.ToInt32(gridViewMain.GetRowCellValue(gridViewMain.FocusedRowHandle, "Id").ToString()); var i2 = db.IncidentJournal.Where(x => x.Id == id).Single(); using (var c = new VGSOContext()) { var i = c.IncidentJournal.Where(x => x.Id == id).Single(); object j = ShowSomething(i); i = (VGSO.Models.IncidentJournal)j; c.IncidentJournal.Attach(i); c.Entry(i).State = EntityState.Modified; c.SaveChanges(); i2 = i; } LoadMainGrid(dateEdit1.DateTime.Year, dateEdit1.DateTime.Month); } 

Second form

 public partial class EditIncidentJournal : Form { VGSOContext db = new VGSOContext(); public object SomeData { get; private set; } public EditIncidentJournal(object somedata) { InitializeComponent(); SomeData = somedata ?? throw new ArgumentNullException(nameof(somedata)); db.rCehs.Load(); db.rObjects.Load(); comboCehs.DataSource = db.rCehs.ToList(); comboObjects.DataSource = db.rObjects.ToList(); } private void EditIncidentJournal_Load(object sender, EventArgs e) { VGSOContext c = new VGSOContext(); if (SomeData is Models.IncidentJournal) { var ss = (Models.IncidentJournal)SomeData; comboCehs.DataBindings.Add(new Binding("SelectedValue", ss, "CehId", false, DataSourceUpdateMode.OnPropertyChanged)); comboObjects.DataBindings.Add(new Binding("SelectedValue", ss, "ObjectId", false, DataSourceUpdateMode.OnPropertyChanged)); MEIncidentType.DataBindings.Add(new Binding("Text", ss, "IncidentType", false, DataSourceUpdateMode.OnPropertyChanged)); DEIncidentTime.DataBindings.Add(new Binding("Datetime", ss, "IncidentDatetime", false, DataSourceUpdateMode.OnPropertyChanged)); MEReason.DataBindings.Add(new Binding("Text", ss, "Reason", false, DataSourceUpdateMode.OnPropertyChanged)); MEIntelligence.DataBindings.Add(new Binding("Text", ss, "Intelligence", false, DataSourceUpdateMode.OnPropertyChanged)); MEDuration.DataBindings.Add(new Binding("Text", ss, "Duration", false, DataSourceUpdateMode.OnPropertyChanged)); MEAmountDamage.DataBindings.Add(new Binding("Text", ss, "AmountDamage", false, DataSourceUpdateMode.OnPropertyChanged)); MEResponsiblePersons.DataBindings.Add(new Binding("Text", ss, "ResponsiblePersons", false, DataSourceUpdateMode.OnPropertyChanged)); } } private void SBSave_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; this.Close(); } private void SBCancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } } 

Please help in understanding. Thank.

  • try db.IncidentJournal.Load(); out db.IncidentJournal.Load(); - 4per

1 answer 1

Doing db.IncidentJournal.Load(); you create a cache of the entire (!) table in the application's memory. Then each time you call your LoadMainGrid() method, you fetch it from the cache. In addition to the unpleasant behavior that you observe, calling Load() for the entire table will also create performance problems as the number of records grows. My opinion is that in your case you just need to remove the call to this method from the code and that's it.