views:

42

answers:

1

Hi,

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 links between the new object and other objects.

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

Can someone solve this problem?

Thanks!

+1  A: 

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.
Craig Stuntz