C
C#•3y ago
kunio_kun

Update Multiple Entities in One Query EF Core

I'm looking for a way to update multiple entities, something that represents
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
I've googled around and answers seems to execute a select-where query first then update each entity using a loop, then update them one by one. Is there a way for this? Thanks in advance
10 Replies
Angius
Angius•3y ago
Select all, loop over them, modify them, save changes Unless you update to EF Core 7, then you can execute bulk updates without fetching
kunio_kun
kunio_kunOP•3y ago
ooh 🤔 so there's no actual way in EF Core 6 to do this without fetching?
Angius
Angius•3y ago
You can do some shenanigans with creating objects with new state and existing IDs, setting them to modified in the change tracker, and saving that But it's janky, and you still need the IDs
kunio_kun
kunio_kunOP•3y ago
in this case, probably meant to be in different question, But i encountered this when I wanted to change an entity's field which is part of the key, but it says
SomeProperty is part of a key and so cannot be modified or marked as modified
SomeProperty is part of a key and so cannot be modified or marked as modified
Is there a way to just do it anyway?
Angius
Angius•3y ago
If it's a key, that means it can be used as a foreign key somewhere else. IIRC EF doesn't support manually editing keys
kunio_kun
kunio_kunOP•3y ago
okay anyway, good to know this
Unless you update to EF Core 7
thanks a lot 😄
kunio_kun
kunio_kunOP•3y ago
Update: I found this https://entityframework-plus.net/ and so far it works nicely for bulk updates
Entity Framework Plus
Learn Entity Framework Plus using EntityFramework Plus by documentation & example
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Angius
Angius•3y ago
Yep
Want results from more Discord servers?
Add your server