1

I wanted to confirm If this is the best way to handle EF6 transactions in .NET API. So if the API changes only a single Entity for example update potato count in user owned potatos. I do not need to enclose it with

using (var dbContextTransaction = context.Database.BeginTransaction()) {}

because the EF will not commit the changes if it can't. But if the single API endpoint updates multiple Entities for expl. API endpoint createSoup() which removes multiple vegitables from user and creates a new entity soup for a usse. Then I do need enclose it with the mentioned statement in case it removes the vegitables, but was unable to create the soup entity and vice versa. Is there no other way? because it seems a little overkill to put all the api endpoint function (which edit mulptile entities) inside this statement.

2
  • How many context.SaveChanges(); do you have? If you have a single call, the you don't need to do anything. Commented Sep 19, 2016 at 7:45
  • There are some operations where there are two of them since the following operation required and entity Id which is not generated until the SaveChanges() is callled :) Commented Sep 19, 2016 at 7:51

1 Answer 1

1

You need to identify the operations on your app where to apply Database.BeginTransaction() method.

The functionality of the Database.BeginTransaction() method is,
you can start and complete transactions themselves within an existing DbContext.In other words it allows several operations to be combined within the same transaction and hence either all committed or all rolled back as one.And It also allows you to specify the isolation level for the transaction.

So if you don't need that functionality,then you can easily use context.SaveChanges() only.As I mentioned earlier you have to clearly identity the operations where you need to apply Transactions.

Note : If you need to handle transactions,then you must use Database.BeginTransaction() method.There is no other way.

Reference : Working with Transactions (EF6 Onwards)

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

5 Comments

Regarding your final note: Another option is to use a TransactionScope.
That is Prior to EF6.It's having lot of limitations.If you're using new Database.BeginTransaction() API then TransactionScope approach is no longer necessary for most users @Maarten
You're warmly welcome :) please see the reference which I have mentioned above @MattJ
@Sampath I know it is a different solution, and yes, it has limitations, but it is an option. Maybe there is no context yet at the point in the code where the two (or multiple) db-context-actions are triggered.
Yes,we can do that.But we must always try to promote to use Latest and Smartest APIs.such APIs will remove huge headaches which we had early days :) @Maarten

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.