使用实体框架插入标识值

发布于 2025-01-07 20:26:30 字数 396 浏览 1 评论 0原文

注:这个问题似乎已经被间接问过好几次了,但从未真正得到回答。

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

但更重要的是,我真的非常希望它具有与删除时完全相同的 ID(我实际上需要从几个与此 ID 无关的表中恢复行)。

是否有任何方法让 EF 将一行放入指定的 Identity 值(但仍保留其他插入的 Identity)。

如果没有什么是解决 EF 进行此插入的好方法(意识到我的 WCF 应用程序可以使用 EF 同时对该表进行插入和更新(尽管具有不同的身份) ))。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掐死时间 2025-01-14 20:26:30

还有哪些其他插入会进入另一个表,需要它们拥有自己的 ID?

无论如何,您应该做的是拥有一个 IDENTITY 列和一个可以为 null 的 int 列,用于存储删除时的 ID。当该信息不相关时,您就不会填充该列。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文