I came across some behaviour within ASP.NET Core 2.2 and EF that I'm not sure if it's intended and especially, if it's safe.
Giving the following code:
{
//...
if (condition)
{
var result = await MyDbContext.MyTable
.Where(data => data.Id == id)
.FirstOrDefaultAsync();
result.MyColumn -= 42;
}
//...more MyDbContext operations
await MyDbContext.SaveChangesAsync();
//...
}
EF Core successfully queries the database and updates the MyColumn field accordingly (substracting 42 in this case), even tho the result
variable declaration and the substraction operation were done in a separate block (and scope implicitly).
Is this an intended and reliable behaviour of how EF operates or it should be avoided and another save operation should be added inside the block?