使用实体框架插入身份值 [英] Insert an Identity Value using Entity Framework

查看:130
本文介绍了使用实体框架插入身份值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这似乎被间接询问了几次,但从未真正回答过。



我有一行使用实体框架删除。当我删除它,我将其存档在不同的表。在某些情况下,该行需要恢复到来自它的表。



但是这里是踢球者,我真的很喜欢它有完全相同的ID有没有删除(我实际上需要从几个表中恢复行,这个ID被关闭)。



有没有任何方式有EF放置一个指定的标识值(但仍然是其他插入的标识)。



如果不是什么是解决这个问题的好方法EF(意识到我的WCF应用程序可以使用EF来同时插入和更新该表格(尽管使用不同的身份))。



我正在使用SQL Server 2008 R2和Entity Framework 4。

解决方案

还有什么其他插入其他表格要求他们拥有自己的ID?



无论如何,你应该做的是有一个IDENTITY列和一个可空的int列t帽子存储删除时的ID。当这些信息不相关时,您只是不填写该列。


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.

解决方案

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.

这篇关于使用实体框架插入身份值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆