Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:35:12+00:00 2026-06-11T10:35:12+00:00

in a case where there may be multiple DbContexts within a custom unit of

  • 0

in a case where there may be multiple DbContexts within a custom unit of work, would EF5 manage all the transactional requirements or is the code below still valid / required? If not, how far can the code be reduced and still provide the same functionality?

public void SaveAllChanges()
{
    var transactions = new List<DbTransaction>();

    foreach (var context in this.contexts
        .Where(context => context != null)
        .Select(dbcontext => ((IObjectContextAdapter)dbcontext).ObjectContext))
    {
        context.Connection.Open();

        var databaseTransaction = context.Connection.BeginTransaction();

        transactions.Add(databaseTransaction);

        try
        {
            context.SaveChanges();
        }
        catch
        {
            foreach (var transaction in transactions)
            {
                try
                {
                    transaction.Rollback();
                }
                finally
                {
                    databaseTransaction.Dispose();
                }
            }

            transactions.Clear();

            throw;
        }
    }

    try
    {
        foreach (var transaction in transactions)
        {
            transaction.Commit();
        }
    }
    finally
    {
        foreach (var transaction in transactions)
        {
            transaction.Dispose();
        }

        transactions.Clear();

        foreach (var context in this.contexts
            .Select(dbcontext => ((IObjectContextAdapter)dbcontext).ObjectContext)
            .Where(context => context.Connection.State != ConnectionState.Closed))
        {
            context.Connection.Close();
        }
    }
}

Assume SQL Server 2008 and above.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:35:14+00:00Added an answer on June 11, 2026 at 10:35 am

    You must handle transactions yourselves when working with multiple contexts but your code is wrong. If any of your transactions fails during commit you can have part of transactions committed and part of transactions rolled back.

    The only valid approach is to use TransactionScope and either manage connections for EF manually or live with distributed transactions:

    var objectContexts = this.contexts
                             .Where(context => context != null)
                             .Select(dbcontext => ((IObjectContextAdapter)dbcontext).ObjectContext))
                             .ToArray();
    
    var transactionOptions = new TransactionOptions { 
        IsolationLevel = IsolationLevel.ReadCommitted 
    };
    using (var scope = new TransactionScope(TransactionScopeOption.Required,
                                            transactionOptions)) {
       Array.ForEach(objectContext, 
                     o => o.SaveChanges(SaveOptions.DetectChangesBeforeSave));
       scope.Complete();
    }
    
    Array.ForEach(objectContext, o => o.AcceptAllChanges());
    

    Here is described why AcceptAllChanges matters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just going to throw all my code in here in case there's something
As an example, say I have a variable defined where there may be multiple
I'm running multiple jobs in jenkins, they all work in the same repo but
The case: There is a .net application calling unmanaged C code. Used method for
I have following use-case: there are several assemblies decorated with ProtoContract classes and I
I have the following use case: There's a class called Template and with that
First I'll lay out what I'm trying to achieve in case there's a different
we are experiencing a few problems with the IDisposable pattern. In this case there
Is there a case when IsChecked property on WPF CheckBox null ? I guess
Is there a case-insensitive variant of the Bob Jenkins hash function? Generics.Defaults.BobJenkinsHash provides a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.