Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to use Transaction in Entity FrameWork? I read some links on Stackoverflow : http://stackoverflow.com/questions/815586/entity-framework-using-transactions-or-savechangesfalse-and-acceptallchanges[^]
But I have 3 table and it means that I have 3 entities:

CREATE TABLE Personel 
(PersonelID integer PRIMARY KEY identity not null, 
Ad varchar(30), 
Soyad varchar(30),
Meslek varchar(100),
DogumTarihi datetime,
DogumYeri nvarchar(100),
PirimToplamı float);
Go

create TABLE Prim
(PrimID integer PRIMARY KEY identity not null,
PersonelID integer Foreign KEY references Personel(PersonelID),
SatisTutari int,
Prim float,
SatisTarihi Datetime);
Go

CREATE TABLE Finans 
(ID integer PRIMARY KEY identity not null, 
Tutar float);


Personel, Prim, Finans are my tables. If you look at Prim table you can see Prim as float value. If I write not float value a textbox my transaction must run.
using (TestEntities testCtx = new TestEntities())
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    // do someyihng...
                    testCtx.Personel.SaveChanges();
                    // do someyihng...
                    testCtx.Prim.SaveChanges();
                    // do someyihng...
                    testCtx.Finans.SaveChanges();
                     scope .Complete();
                        success = true;

                }
            }

How can I do that?
Posted
Updated 24-Mar-11 4:15am
v3

Hi,

Your approach looks ok, but while using the SaveChanges on top of ObjectContext instance, i would prefer you to use SaveChanges(false) than SaveChanges() -

The reason behind that is if one of the operation inside the transaction results in error, then your context wont loose the states of an entity if you use SaveChanges(false).

Hence you can continue/take corrective action in case of any error which is not the case with SaveChanges().


I hope this helps!.

Regards,
-Vinayak
 
Share this answer
 
Comments
Dalek Dave 24-Mar-11 10:25am    
Good Answer
Check this sample application:

Entity Framework for Beginners[^]
 
Share this answer
 
v2
I also have a question about EF transaction!

I want to set all activities in my window as transaction so I can't use using statement because end using will be in another function. How will I do?

You maybe wonder why I need that!

Because this window use the same context than it parent window, I've just passed context to it like following
VB
Private Sub EditDetailPlan() Handles btnDetailsPlan.Click
    Dim Win As New WinPlanningDetails
    Win.Context = Context
    Win.Owner = PlanningWizardStep3.ParentWindow
    Win.ShowDialog()
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900