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

  • Home
  • SEARCH
  • 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 4036122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:11:03+00:00 2026-05-20T12:11:03+00:00

I created very a simple database using Entity Framework 4. I’d like to be

  • 0

I created very a simple database using Entity Framework 4. I’d like to be able to use transactions on entities, but I can’t seem to keep changes from rolling back. I really just need a way to abandon temporary changes to entities before they are saved to the database.

For example, the following code uses an entity framework object context “MusicContainer”. Inside a TransactionScope, an Artist entity is created. The transaction then ends without being completed; so I’d expect the transaction to be rolled back. But, the program runs as if I’d never created the TransactionScope in the first place; after the TransactionScope ends, the line music.SaveChanges() saves the object to the database.

class Program
{
    static void Main(string[] args)
    {
        using (MusicContainer music = new MusicContainer())
        {
            using (TransactionScope transaction = new TransactionScope())
            {
                Artist artist = new Artist { Name = "Test" };
                music.Artists.AddObject(artist);
            }
            // The transaction ended without Complete(); shouldn't the changes be abandoned?
            music.SaveChanges();
        }
    }
}

If entity framework doesn’t use TransactionScope the way I’m expecting it to here, how can I get the functionality I’m looking for? I have several circumstances where the caller of a function passes in the MusicContainer, and I need to leave the MusicContainer in a clean state before I return from the function (i.e. rolling back changes so they don’t accidently get saved in with another SaveChanges called on the same MusicContainer object).

  • 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-05-20T12:11:04+00:00Added an answer on May 20, 2026 at 12:11 pm

    You don’t need a TransactionScope at all in this scenario, SaveChanges() is all that’s needed – if you have another using block with MusicContainer, this will be in a separate transaction and won’t save any changes within your current using block. TransactionScope is only needed for transactions spanning multiple DB contexts.

    In general a context should only be used for a unit of work consisting of related operations, once they are completed call SaveChanges(). Open a new context for each separate, unrelated unit of work. Having said that, just use this in your scenario:

            using (MusicContainer music = new MusicContainer())
            {
                    Artist artist = new Artist { Name = "Test" };
                    music.Artists.AddObject(artist);
                    music.SaveChanges();
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a very simple app, which presents an easygui entrybox() and continues to
Suppose I create a very simple socket connection, how can one programatically: Find out
I've created a custom exception for a very specific problem that can go wrong.
I was developing an application using Entity Framework and storing data in a .mdf
I am trying a very simple Fluent Nhibernate example: SQL 2005 database with one
I created an application using GWT and support for the Google Datastore but now
I want to create a very simple HTML/AJAX based GUI for a Python program.
I wish to create a very simple web app that simply displays an Image(s)
My goal here is to create a very simple template language. At the moment,
i have very simple problem. I need to create model, that represent element of

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.