开发者

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

开发者 https://www.devze.com 2023-01-08 05:52 出处:网络
I have table with sequence and trigger so it creates new id when inserting it to the DB. How can I get the new object id, before calling SaveChanges()?

I have table with sequence and trigger so it creates new id when inserting it to the DB.

How can I get the new object id, before calling SaveChanges()?

I need this id for the link开发者_如何学Pythons between the new object and other objects.

I want to do it before calling SaveChanges because I want to have the option to rollback.


You can't get it without calling SaveChanges(). Think about it. The ID comes from the DB. SaveChanges() is the first thing which causes communication with the 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. Simply creating the relationship is enough.
  • Use a client-generateable ID like a GUID.
  • Use TransactionScope for a "real" DB transaction.
  • Do an undo (DeleteObject) instead of a rollback.
0

精彩评论

暂无评论...
验证码 换一张
取 消