我有一個名爲「Posts」的數據庫表,它存儲了有關網站上文章提交的所有信息。有一個名爲「視圖」的列,它是每次查看特定帖子時都會增加的值。更新數據庫中網頁的查看次數
的過程是這樣的:
- 從數據庫
- 遞增當前獲取的記錄由一個
- 將更改保存到數據庫中。
很簡單。我擔心的是,如果多人同時點擊鏈接,更新不會是準確的。我應該如何處理這個問題?這應該只在存儲過程中完成嗎?
/// <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);
}
所以我不應該這樣做的代碼?是最好的一個sproc嗎? – Micah 2008-12-18 00:13:39