I'm adding a set of objects using entity framework in single context but want to be in a transaction. How I can do that?
3 Answers
You may take a look at the How to: Manage Transactions in the Entity Framework article on MSDN. Basically you would use the TransactionScope class to demarcate the boundaries of the transaction.
1 Comment
VJAI
Currently I'm using TransactionScope. Is it affects performance?
If you call SaveChanges only once for all your changes you already have transaction because SaveChanges internally checks if the thread has associated transaction (for example via TransactionScope) and if it doesn't it will internally create a new one by calling connection.BeginTransaction.
2 Comments
VJAI
The problem I'm facing is I'll create a Template object and following TemplateLayouts object that has a property as TemplateId. To get the TemplateId first I've to call the SaveChanges after the AddObject method is called then one more SaveChanges after all the layouts are added. I want to combine all in single transaction.
Ladislav Mrnka
Then you are doing it wrong. Your TemplateLayout should have Template property and by setting this property to a new Template instance EF will handle everything else.