views:

34

answers:

1

I am using Entity Framework and I've come to an interesting stumbling block. Let's say there is a db table "Item" with "sequence" column of type int (and others of course). Column "sequence" must be unique and it is used for (re)ordering of items.

EF maps this table to "Item" class with "sequence" int property. Now let's say I want to swap position of two items by mutually exchanging each other's sequence number. Upon calling SaveChanges() EF throws an exception complaining about "sequence" uniqueness. It probably generates two UPDATEs and the first one probably fails.

I assume that plain SQL solution to this issue is using a third UPDATE to introduce a unique sequence value in the process but I am stuck with EF.

Any thoughts?

EDIT: I am using SQL Express.

+1  A: 

If you are using SQL Server Express 2008 you could explore using the new T-SQL MERGE statement. You should be able to update the sequence column with a single MERGE statement.

ktingle
Yes, I am using SQL Server Express 2008 and even if MERGE statement did the trick as you say, is there any cure for Entity Framework? I can't go back to writing plain SQL now.
wpfwannabe
Sure you can, you have ExecuteStoreQuery or stored procedures at your disposal. The problem you are facing is really a modeling issue, if the data modeler who created your database had known the column was editable your problem could have been avoided.
ktingle
Ok, but avoided how exactly?
wpfwannabe