SaveChangesAsync does not update the value in the database table

This is my table: Statistics

Id,Depth,RefreshCounter 

Examples of entries:

 Id Depth RefreshCounter 1 1 1 2 1 0 3 1 0 4 1 0 

Now I need to do when I refresh the page, I need to increase this refreshcounter value by 1 in the database table using Depth 1 .

I call this method as follows when loading my view page:

 @Html.Action("IncrementRefreshcounter", "Statistics", new { id = 1}) //for eg:1,2,3,4 

Here is my code that does this:

  [ChildActionOnly] public ActionResult IncrementRefreshcounter(int id) { using ( var context = new MyDBContext()) { //at each page refresh i would be passing different id to my controller from view.for eg 1,2,3,4 var data=context.Statistics.where(x=>x.Depth==1 && r.Id==id).FirstorDefualt(); context.RefreshCounter++;//Increment here RefreshCounter. context.SaveChangesAsync(); return PartialView("xxx") } } 

I call this method when my View Loads.Problem is when I launch my application for the first time and call this method, it successfully updated RefreshCounter to 1, but after that, when I refresh my page and calling this method, it never refreshes RefreshCounter for any record with depth = 1.

In my recording example, you can see that Id 1 with Depth 1 has an update counter with a value of 1, because this was the first time I launched my application and it successfully updated this value, but after that it never updates the value for example: Id 2 Depth 1

It increases only 1 time by RefreshCounter, but after that it never increases this variable.

Can someone tell me what the problem is SaveChangesAsync ??

+6
source share
2 answers

You must wait for the save, otherwise the method will continue and your context will go out of scope before the changes are saved. You must also make the method asynchronous.

 public **async** Task<ActionResult> IncrementRefreshcounter(int id) { using ( var context = new MyDBContext()) { //at each page refresh i would be passing different id to my controller from view.for eg 1,2,3,4 var data=context.Statistics.where(x=>x.Depth==1 && r.Id==id).FirstorDefualt(); context.RefreshCounter++;//Increment here RefreshCounter. await context.SaveChangesAsync(); } } 
+8
source

Try the following:

  public async Task<ActionResult> IncrementRefreshcounter(int id) { using ( var context = new MyDBContext()) { //at each page refresh i would be passing different id to my controller from view.for eg 1,2,3,4 var data=context.Statistics.where(x=>x.Depth==1 && r.Id==id).FirstorDefualt(); context.RefreshCounter++;//Increment here RefreshCounter. await context.SaveChangesAsync(); } } 

SaveChanges will execute in the current thread and block the thread while waiting for the request to complete, preventing the thread from doing other work, even if the thread just sits there, waiting for I / O.

SaveChangesAsync runs an I / O command and then frees the request stream to do other work while I / O is running. After I / O is completed, the rest of the method runs in the captured synchronization context.

So, for a web server using asynchronous APIs to work with IO binding, you can serve more requests with fewer threads and thus make your application more scalable and use much less memory, and each thread has 1 MB of stack space default.

Synchronous operation is similar to the SaveChanges method. The method call is not returned until the changes are saved.

Asynchronous operation is similar to SaveChangesAsync , a method call initiates an operation and returns a Task so you can track it. These changes are then saved in the background some time later, and at the same time, you can use the returned Task to track the operation.

+7
source

Source: https://habr.com/ru/post/987715/


All Articles