Closed rowanmiller closed 7 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?
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.
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?
Any idea? Thanks.
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
Thank you so much for leading me, and sorry for not finding the document before. Have a nice day.
Closing as we have docs now - https://docs.efproject.net/en/latest/saving/transactions.html
@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!
@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!
@xyting I don't have demo code.
@hikalkan Have any opinion about @rowanmiller point DbConnection.EnlistTransaction(...)
?
Maybe he is saying UseTransaction. He can answer better :)
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.
In particular, how to achieve scenarios that would previously have been done with TransactionScope