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 8496233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:47:44+00:00 2026-06-10T23:47:44+00:00

I have a code like this: public abstract class DataContextBase { public DbContext DbContext

  • 0

I have a code like this:

public abstract class DataContextBase
{
    public DbContext DbContext { get; protected internal set; }
    public ObjectContext ObjectContext { get; protected internal set; }
    protected DbTransaction transaction;

    protected void SetContext(DbContext db, ObjectContext oc)
    {
        DbContext = db;
        ObjectContext = oc;
    }

    public void BeginTransaction()
    {
        if (ObjectContext.Connection.State != System.Data.ConnectionState.Open)
        {
            ObjectContext.Connection.Open();
        }
        transaction = ObjectContext.Connection.BeginTransaction();
    }

    public void CommitTransaction()
    {
        try
        {
            transaction.Commit();
        }
        finally
        {
            transaction = null;
            ObjectContext.Connection.Close();
        }
    }

    public void RollbackTransaction()
    {
        try
        {
            transaction.Rollback();
        }
        finally
        {
            transaction = null;
            ObjectContext.Connection.Close();
        }
    }

    public void Save()
    {
        DbContext.SaveChanges();
    }
}

It is from a sample application, and I use this as a base class of my application’s main data context. I’m using Entity Framework 5, and I have just read that when I call the DbContext’s SaveChanges method, it always runs in a database transaction and it will throw an exception when the transaction have to be rollbacked and in this case the changes are not saved into the database.

But in the sample application, almost every service method begins with a DataContextBase.BeginTransaction call and ends with a DataContextBase.CommitTransaction call (in an exceptional case it ends with DataContextBase.RollbackTransaction) even though that DataContextBase.Save is called (which calls DbContext.SaveChanges()).

It looks like there is an extra transaction wrapping the built in transaction of the DbContext.SaveChanges call.

Could there be any situation which needs this extra transaction?

NOTE: The DataContextBase’s ObjectContext is come from the DbContext with a trick:

((IObjectContextAdapter)this).ObjectContext; // inside the DbContext class
  • 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-10T23:47:45+00:00Added an answer on June 10, 2026 at 11:47 pm

    Having an extra transaction is redundant because ObjectContext/DbContext implements Unit of Work. If you have other means of communicating with the database and they also need to be part of the transaction the use TransactionScope.

    Connection management is also done by EF and you do not have to

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

Sidebar

Related Questions

I have a code first application which defined like this: public abstract class Entity
Let's say I have code that looks like this: public abstract class ResourceDownloaderBaseImpl implements
I have code like this: public bool Set(IEnumerable<WhiteForest.Common.Entities.Projections.RequestProjection> requests) { var documentSession = _documentStore.OpenSession();
I have a code like this: class PacketDAO{ //... public void UpdatePacketStatus(Guid packetID, Status
I have a similar code snippet like this class Search { public function search($for,
I have code that looks like this: template<class T> class list { public: class
I have something like this: public abstract class Menu { public Menu() { init();
Consider this abstract class public abstract class Foo { public Injectable Prop {get;set;} }
I have following code: public abstract class Operand<T> { public T Value { get;
If I have a C# code like this: public class A { public string

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.