I created a very simple database using Entity Framework 4. I would like to be able to use transactions for entities, but I can't help but undo the changes. I really need a way to discard temporary changes to objects before they are saved to the database.
For example, the following code uses the context object object of the "MusicContainer" entity. Inside the TransactionScope, an Artist object is created. Then the transaction ends without completion; therefore, I expect the transaction to be canceled. But the program works as if I had never created a TransactionScope; after the TransactionScope completes, the music.SaveChanges () line stores the object in 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);
}
music.SaveChanges();
}
}
}
TransactionScope , , , ? , MusicContainer, MusicContainer , (, , SaveChanges, MusicContainer).