Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple rows update without select

An old question for Linq 2 Entities. I'm just asking it again, in case someone has came up with the solution.

I want to perform query that does this:

UPDATE dbo.Products WHERE Category = 1 SET Category = 5

And I want to do it with Entity Framework 4.3.1.

This is just an example, I have a tons of records I just want 1 column to change value, nothing else. Loading to DbContext with Where(...).Select(...), changing all elements, and then saving with SaveChanges() does not work well for me.

Should I stick with ExecuteCommand and send direct query as it is written above (of course make it reusable) or is there another nice way to do it from Linq 2 Entities / Fluent.

Thanks!

like image 524
Admir Tuzović Avatar asked Mar 14 '26 05:03

Admir Tuzović


1 Answers

What you are describing isnt actually possible with Entity Framework. You have a few options,

  1. You can write it as a string and execute it via EF with .ExecuteSqlCommand (on the context)
  2. You can use something like Entity Framework Extended (however from what ive seen this doesnt have great performance)
like image 97
Not loved Avatar answered Mar 16 '26 22:03

Not loved



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!