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).