3

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 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Currently I'm using TransactionScope. Is it affects performance?
2

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

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.
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.
0

You can create transaction object on context connection object, then you can use transaction commit or rollback with regular mechanism or transaction scope.

dbContext.Connection.BeginTransaction()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.