Insert an Identity Value using Entity Framework

倾然丶 夕夏残阳落幕 提交于 2019-12-12 02:28:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!