I have a database table called Messages, which stores all the information about the presentation of an article on a website. There is a column called “Views”, which is a value that increases each time you view a specific message.
The process is as follows:
- Get record from database
- Increase current by one
- Save the changes to the database.
Pretty simple. I am worried that if several people click the link at the same time, the updates will not be accurate. How do I approach this? Should this only be done in a stored procedure?
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);
}
Micah source
share