With EF Core: Is there a way to tell it that the new value in the single field I update is the same for all entities and make it update the entities with one/few SQL statements?
I want to set a timestamp on a lot of records at the same time. I currently have this code:
var downloadTimeStamp = DateTime.UtcNow;
foreach (var e in entities)
{
e.DownloadDateTime = downloadTimeStamp;
}
await db.SaveChangesAsync();
It generates SQL that executes one UPDATE statement for each entity. E.g.:
UPDATE [TableName] SET [DownloadDateTime] = @p1988
WHERE [Id] = @p1989;
SELECT @@ROWCOUNT;
Is that really the most optimal way to do this? Is it possible to make EF Core do this in fewer SQL statements?
In my current dataset there are 100,000 records that need to be updated. It is not the full contents of the table, only a subset. It takes about 90 seconds to do the above updates when running the code inside Visual Studio with a SQL server express on my local PC. When running in a small Azure web app with a small SQL server Azure instance, it takes several minutes. That's why I'm looking to optimize it, if possible.