I'm running into a problem when updating some data via EF.
Let's say I have a table in my database:
Table T (ID int, Rank int, Name varchar)
I have a unique key constraint on Rank.
For example, I have this data in the table:
My C# object is something like this: Person (name, rank), so on the front end, a user wants to switch the rank of Joe and Mark.
When I make the update via EF, I get an error because of the unique key.
I suspect it is because dbContext.SaveChanges uses a update in this style:
UPDATE Table SET rank = 5 where Name = Joe
UPDATE Table SET rank = 1 where Name = Mark
With a SQL query I can perform this update by doing this:
Pass in User Defined table (rank, name) from C# side into query and then:
update T
set T.Rank = Updated.Rank
from Table T
inner join @UserDefinedTable Updated on T.Name = Temp.Name
and this does not trigger the unique key constraint
However I want to use EF for this operation, what do I do?
I've thought of these other solutions so far:
Delete old records, add "new" records from updated objects via EF
Dropping the unique constraint on database and writing a C# function to do the job of the unique constraint
Just use a SQL query like the example above instead of EF
Note: the table structure and data I used above is just an example
Any ideas?
