SaveChangesAsync不更新数据库表中的值 [英] SaveChangesAsync not updating value in database table

查看:1753
本文介绍了SaveChangesAsync不更新数据库表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的表:统计

  Id,Depth,RefreshCounter 

样本记录:

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

现在我需要做的是每当我刷新页面时,我需要在深度1 的数据库表中将这个刷新值增加1。 >

我在我的浏览页面上加载了这样的方法:

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

这是我的代码:

  [ChildActionOnly] 
public ActionResult IncrementRefreshcounter(int id)
{
using(var context = new MyDBContext())
{
//在每个页面刷新我将是pa从我的控制器中查看不同的id,例如1,2,3,4
var data = context.Statistics.where(x => x.Depth == 1&&& r.Id == ID).FirstorDefualt();
context.RefreshCounter ++; //在这里增加RefreshCounter。
context.SaveChangesAsync();
返回PartialView(xxx)
}
}

当我的View Loads.Problem是我第一次运行我的应用程序时,我调用这个方法,并调用这个方法,它成功地更新了RefreshCounter 1,但之后,当我刷新我的页面,并调用这个方法,它从未更新RefreshCounter任何记录与深度= 1。



在我的示例记录中,您可以看到具有深度1的Id 1具有值为1的刷新计数器,因为这是我第一次运行我的应用程序它已经成功地更新了该值,但之后它从不更新任何值,例如:Id 2 Depth 1



它只增加1次RefreshCounter,但之后它不会递增那个变量。



有人可以告诉我什么问题是 SaveChangesAsync

解决方案

你必须等待保存,否则该方法将继续在保存更改之前,您的上下文将超出范围。你必须使方法是异步的。

  public ** async ** Task< ActionResult> IncrementRefreshcounter(int id)
{
using(var context = new MyDBContext())
{
//在每个页面刷新我将传递不同的ID到我的控制器从视图例如1,2,3,4
var data = context.Statistics.where(x => x.Depth == 1&& r.Id == id).FirstorDefualt();
context.RefreshCounter ++; //在这里增加RefreshCounter。
等待context.SaveChangesAsync();
}
}


This is my Table:Statistics

Id,Depth,RefreshCounter

Sample Records:

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

Now what i need to do is whenever i am refreshing the page i need to increment this refreshcounter value by 1 in Database table with Depth 1.

I am calling this method like this on load of my view page:

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

Here is my code which 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 am calling this method when my View Loads.Problem is when i run my Application first time and calling this method it successfully updated RefreshCounter by 1 but after that whenever i refresh my page and calling this method it never updated RefreshCounter for any records with Depth=1.

In my sample Records you can see that Id 1 with Depth 1 have refresh counter with value 1 because it was the first time when i have run my application and it has successfully updated that value but after that it is never updating any value for eg:Id 2 Depth 1

It is incrementing only 1 time RefreshCounter but after that it never increments that variable.

Can anybody tell me what the problem is SaveChangesAsync ??

解决方案

You have to await the save otherwise the method will continue and your context would go out of scope before saving changes. You have to make the method asynchronous, too.

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();
            }
        }

这篇关于SaveChangesAsync不更新数据库表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆