I am using EF Core 3.1 have the following code
public void Update()
{
Client client = new Client();
client.ClientId = 1;
client.Name = "New Client 1";
dbContext.Entry<Client>(client).State = EntityState.Modified;
dbContext.Entry<Client>(client).Property(x => x.IdentityNumber).IsModified = false;
dbContext.SaveChanges();
}
which produces the following SQL an successfully updates the column
UPDATE [Client]
SET [Name] = @p0
WHERE [ClientId] = @p1;
SELECT @@ROWCOUNT;
However when I change the ClientId = 4
which is an entry that was deleted from database it produces the same SQL query but throws the following error
Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded
The error message is clear but I don't understand why it does not work as SQL Server works (0 row(s) affected) and instead throws this error.
How can I simulate the 0 row(s) affected SQL Server behavior using EF Core?
I do not want to query the database first if the row exists and then update. The above code is a sample. In my methods I have lists with many clients that I need to update at once and it takes long to do that using the first check if exists and then update.
I do not want to query the database first if the row exists and then update.
This is the crux of your problem and the reason for the error.
Our approach, and a common one I believe, is to use Dapper when manipulating a lot of data, and limit EF to Load -> Edit -> Save operations on single or very few entities.