[英]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: 如果要撤消,您有以下几种选择:
TransactionScope
for a "real" DB transaction. TransactionScope
进行“真正的”数据库事务。 DeleteObject
) instead of a rollback. DeleteObject
)而不是回滚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.