dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.59k stars 1.95k forks source link

Working with Transactions #108

Closed rowanmiller closed 7 years ago

rowanmiller commented 8 years ago

In particular, how to achieve scenarios that would previously have been done with TransactionScope

ElvisHalil commented 8 years ago

Looking for exactly this. I am using repository pattern with EF 7 and have a method where all my DB updates happen. I'd like to wrap all of these DB calls (Repository.Add + SaveChanges) into a single transaction so if anything fails everything rolls back for all repositories for that particular set of data.

With what I've read it's currently not supported. Any work around at this time?

rowanmiller commented 8 years ago

In RC1 you would need to use context.Database.BeginTransaction()/UseTransaction() to setup using the same transaction for multiple contexts. If all the data is being saved thru a single context instance, then it is already transactional.

hikalkan commented 8 years ago

Hi,

We may have also these scenarios:

For such requirements, how can we create a transaction that will be shared by all these contexes :) Will EF Core support that?

hikalkan commented 7 years ago

Any idea? Thanks.

rowanmiller commented 7 years ago

Hey,

First bullet point is covered in https://docs.efproject.net/en/latest/saving/transactions.html#cross-context-transaction-relational-databases-only. The other two would use the same technique, except you would need to use DbConnection.EnlistTransaction(...) since you would need two separate connections. The one disclaimer is that I haven't actually tried that, but I see no reason that it shouldn't work.

~Rowan

hikalkan commented 7 years ago

Thank you so much for leading me, and sorry for not finding the document before. Have a nice day.

rowanmiller commented 7 years ago

Closing as we have docs now - https://docs.efproject.net/en/latest/saving/transactions.html

rigofunc commented 7 years ago

@rowanmiller

Have any Multiple dbcontext type with multiple database (each dbcontext targets to a different database, in same server) demo code? I have not found any about your say DbConnection.EnlistTransaction(...)

hope your help, thanks!

rigofunc commented 7 years ago

@hikalkan

Have any about Multiple dbcontext type with multiple database (each dbcontext targets to a different database, in same server) demo code? or more help links?

hope your help, thanks!

hikalkan commented 7 years ago

@xyting I don't have demo code.

rigofunc commented 7 years ago

@hikalkan Have any opinion about @rowanmiller point DbConnection.EnlistTransaction(...)?

hikalkan commented 7 years ago

Maybe he is saying UseTransaction. He can answer better :)

rowanmiller commented 7 years ago

Via the Database APIs you can only share a single transaction if you use the same connection, which won't work if you are targeting different databases. You probably want to spin up two transactions and then call Commit() on both once the operations succeed.