I need some idea in handling transactions in Entity Framework.
Let’s consider a small example.
In my database I have a table A with auto generated identity column id, and I have a table B with a reference key to A(id).
In a scenario where I need to insert data in to both tables A and B, I want to start a transaction. Lets say a new row is inserted into A. I need newly inserted identity (id) value that I need to utilize for B insertion.
Can someone give me lead on handling this situation? Do we need to really utilize transactions in this case?
When you call SaveChanges, the updates are made in a transaction. If one fails, it is all rolled back. See here on msdn. Particularly the “Remarks” section.