Answer the question
In order to leave comments, you need to log in
How to make group update in entity framework so as not to break constarint?
Please, prompt
There is a table, in it there is a field, with restriction UNIQUE.
I need to update this table.
If everything is done in pure sql, then there is no problem, because the update occurs in one request.
But through EF it doesn’t work, because the lines are updated one by one.
Example:
The year field is unique. I want to put 2019 in the entry where 2020 stood, and vice versa, in short, swap places.
Here is a working example in sql.
UPDATE table
SET year = (CASE WHEN year = 2019 THEN 2020 WHEN year = 2020 THEN 2019 END)
WHERE year IN (2019, 2020)
Answer the question
In order to leave comments, you need to log in
Emnip, EF does not allow updating many records with one request. use sql
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question