【发布时间】:2008-12-18 00:04:24
【问题描述】:
我有一个名为“Posts”的数据库表,它存储有关网站上文章提交的所有信息。有一个名为“Views”的列,每次查看特定帖子时都会增加一个值。
流程是这样的:
- 从数据库中获取记录
- 电流加一
- 将更改保存到数据库。
非常简单。我担心的是,如果多个人同时单击该链接,则更新将不准确。我应该如何处理这个?这应该只在存储过程中完成吗?
/// <summary>
/// Updates the view count of a post.
/// </summary>
/// <param name="postId">The Id of the post to update</param>
public bool UpdateViewCount(int postId)
{
Repository repository = new Repository();
Post p = repository.Posts.Where(p => p.Id == postId).SingleOrDefault();
if (p != null)
{
p.Views++;
}
repository.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
}
【问题讨论】:
标签: c# .net asp.net asp.net-mvc