简体   繁体   English

如何获取DB序列生成的新对象ID?

[英]How can I get the new object id generated by DB sequence?

I have table with sequence and trigger so it creates new id when inserting it to the DB. 我有表序列和触发器,因此在将其插入数据库时​​会创建新的id。

How can I get the new object id, before calling SaveChanges()? 在调用SaveChanges()之前,如何获取新的对象id?

I need this id for the links between the new object and other objects. 我需要这个id用于新对象和其他对象之间的链接。

I want to do it before calling SaveChanges because I want to have the option to rollback. 我想在调用SaveChanges之前这样做,因为我想要回滚选项。

You can't get it without calling SaveChanges() . 如果不调用SaveChanges()你就无法得到它。 Think about it. 想一想。 The ID comes from the DB. ID来自数据库。 SaveChanges() is the first thing which causes communication with the DB. SaveChanges()是导致与DB通信的第一件事。

If you want to undo, you have several choices: 如果要撤消,您有以下几种选择:

  • In many cases, you don't need to know the ID client-side when inserting. 在许多情况下,插入时无需知道ID客户端。 Simply creating the relationship is enough. 简单地创建关系就足够了。
  • Use a client-generateable ID like a GUID. 使用客户端生成的ID,如GUID。
  • Use TransactionScope for a "real" DB transaction. 使用TransactionScope进行“真正的”数据库事务。
  • Do an undo ( DeleteObject ) instead of a rollback. 执行撤消( DeleteObject )而不是回滚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Linq2DB 找到使用 Oracle 序列 NextVal 方法创建的新 object 的 ID? - How can I find the ID of a new object created using the Oracle sequence NextVal method using Linq2DB? 如何获取动态生成的控件的ID? - How can I get ID's of Dynamically generated Controls? 如何获取自动生成的ID? - How do I get the auto generated Id? 我可以在Linq生成的传入新对象中使用get / set方法吗? - Can I use get/set method inside a incoming new object generated by Linq? 在 SaveChangesAsync() 之后如何从对象中获取 ID - How can i get the ID from an Object after SaveChangesAsync() 如何获取列表中的值 <object> 通过最高ID? - How can I get a value in a List<object> by highest Id? EF如何获取系统生成的新添加对象的ID - How EF will get the system generated ID of a newly added object 如何从 reactJS 或 C# 中的 JSON 获取自动生成的 ID - How can i get auto generated id from JSON in reactJS or C# 我如何获得从OrderBy / GroupBy生成的新表的主键? - How can i get primary of a new table generated from OrderBy / GroupBy? 如果我有 ZA8CFDE6331BD59EB2AC96F8,如何使用 REST API 在 AzureAD 中获取 object 的显示名称? - How can I get the display name of an object in AzureAD using REST API if I have object ID?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM