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.
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.
精彩评论