Reputation: 213
I'd like to make EF generate a SQL query like this:
UPDATE Users
SET CurrentTurn = CurrentTurn + 1
WHERE id = 1
In the current implementation I have to use two queries:
public void MakeTurn(int userId)
{
var user = _storageContext.Users.Find(userId);
user.CurrentTurn++;
_storageContext.SaveChanges();
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int CurrentTurn { get; set; }
}
Upvotes: 1
Views: 103
Reputation: 39346
If you want to do it in one DB roundtrip as far I know you'll need to install a 3rd party library like EntityFramework-Plus:
_storageContext.Users.Where(x => x.Id==userId)
.Update(x => new User() { CurrentTurn = x.CurrentTurn+1 });
In the github page you will find the instruction about the nuget package you'll need to install, in you case is:
PM> Install-Package Z.EntityFramework.Plus.EFCore
If you want to see other options you can check this link, but I always have use the quoted library in cases like yours
Upvotes: 2