Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Incrementing column value without query it before

I have a sql server with the table "contacts", in this table i have column "clicks" of type int, i want to increment its value without making a query.

This is a copy of: "Entity framework update one column by increasing the current value by one without select" the solution that suggested there is using EntityFramework.Utilities the problem is that its not a IQueryable extension like EntityFramework.Extended and that makes it hard to implement for me.

the question is there any other way/solution to incremenet int value on sql with using IQueryable(without actually writing sql query)?

the result of the IQueryable query should look like:

UPDATE Contacts SET clicks=clicks+1 WHERE id=8432
like image 922
HaimYuhter Avatar asked Dec 29 '16 08:12

HaimYuhter


1 Answers

I just found the solution after some research and testing, actually EntityFramework.Extended do exactly what i want it to do, if you want to increment a column just call the Update method with adding assignment:

db.Contacts.Where(c => c.Id == id).Update(c => new Contact { Clicks = c.Clicks + 1 });

After checking the source code of EntityFramework.Extended and debugging it i saw that the query that executed in the end is exactly what i wanted, so thanks everybody for trying to help, the solution were right under my nose ;-)

like image 88
HaimYuhter Avatar answered Nov 13 '22 10:11

HaimYuhter