Answer the question
In order to leave comments, you need to log in
How to modify only one element field using Entity Framework?
There is a method
public void IncreaseArticleViews(int ArticleId)
{
context.Articles.FirstOrDefault(p => p.ArticleId == ArticleId).CountViews += 1;
context.SaveChanges();
}
Answer the question
In order to leave comments, you need to log in
var article = context.Articles.FirstOrDefault(p => p.ArticleId == ArticleId);
int countViews = article.newCountViews++;
context.Database.ExecuteSqlCommand("UPDATE dbo.Articles SET CountViews = {0} WHERE ArticleId = {1}", countViews , ArticleId);
I would supplement the answer from Anvar Shakhmaev @pro100gram
Why do we need to download the entire object from the database, download only the counter)
Instead of:
I would make:
var article = context.Articles.FirstOrDefault(p => p.ArticleId == ArticleId).Select(p => p.CountViews);
int countViews = article++;
context.Database.ExecuteSqlCommand("UPDATE dbo.Articles SET CountViews = {0} WHERE ArticleId = {1}", countViews , ArticleId);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question