I have a database table called "Posts" which stores all the information regarding an article submission on a website. There is a column named "Views" which is a value that gets incremented each time that particular post gets viewed.
The process is this:
- Get the record from the database
- Increment the current by one
- Save the changes to the database.
Pretty straightforward. My concern is that if multiple people click the link at the same time, the updates wont be accurate. How should I approach this? Should this only be done ina stored procedure?
/// <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);
}