Coder Social home page Coder Social logo

Comments (13)

ElvisHalil avatar ElvisHalil commented on June 3, 2024

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?

from entityframework.docs.

rowanmiller avatar rowanmiller commented on June 3, 2024

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.

from entityframework.docs.

hikalkan avatar hikalkan commented on June 3, 2024

Hi,

We may have also these scenarios:

  • Multiple dbcontext type with single connection string (same database, different contexes)
  • Multiple dbcontext type with multiple database (each dbcontext targets to a different database, in different server or same server)
  • Single dbcontext type with multiple database (for example, a multi-tenant app which has a single dbcontext type but each tenant has it's own database), so there will be multiple instances of same dbcontext type in a unit of work

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

from entityframework.docs.

hikalkan avatar hikalkan commented on June 3, 2024

Any idea? Thanks.

from entityframework.docs.

rowanmiller avatar rowanmiller commented on June 3, 2024

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

from entityframework.docs.

hikalkan avatar hikalkan commented on June 3, 2024

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

from entityframework.docs.

rowanmiller avatar rowanmiller commented on June 3, 2024

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

from entityframework.docs.

rigofunc avatar rigofunc commented on June 3, 2024

@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!

from entityframework.docs.

rigofunc avatar rigofunc commented on June 3, 2024

@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!

from entityframework.docs.

hikalkan avatar hikalkan commented on June 3, 2024

@xyting I don't have demo code.

from entityframework.docs.

rigofunc avatar rigofunc commented on June 3, 2024

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

from entityframework.docs.

hikalkan avatar hikalkan commented on June 3, 2024

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

from entityframework.docs.

rowanmiller avatar rowanmiller commented on June 3, 2024

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.

from entityframework.docs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.