问题
Note: This seems to have been asked indirectly several times, but never really answered.
I have a row that I delete using Entity Framework. When I delete it I archive it in a different table. In some cases that row needs to be restored to the table it came from.
But here is the kicker, I would really really like it to have the exact same ID it had when it was deleted (I actually need to restore rows from several tables that are FKed off this ID).
Is there any way to have EF put a row in with a specified Identity value (yet still remain an Identity for other inserts).
If not what would be a good way to work around EF to make this insert (realizing that my WCF app could be using EF to make inserts and updates to that table at the same time (though with different identities)).
I am using SQL Server 2008 R2 and Entity Framework 4.
回答1:
What other inserts would be going into this other table that require them to have their own IDs?
Anyway what you should do is have an IDENTITY column and a nullable int column that stores the ID of what that was when it was deleted. When that information is not relevant then you just don't populate that column.
来源:https://stackoverflow.com/questions/9405280/insert-an-identity-value-using-entity-framework