Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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 14-Apr-10 4:07am
Edited 24-Mar-11 4:15am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Dalek Dave at 24-Mar-11 10:25am
   
Good Links
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Dalek Dave at 24-Mar-11 10:25am
   
Good Answer
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
Private Sub EditDetailPlan() Handles btnDetailsPlan.Click
    Dim Win As New WinPlanningDetails
    Win.Context = Context
    Win.Owner = PlanningWizardStep3.ParentWindow
    Win.ShowDialog()
End Sub
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Check this sample application:
 
Entity Framework for Beginners[^]
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 210
1 Richard MacCutchan 100
2 kbrandwijk 90
3 ChauhanAjay 80
4 ProgramFOX 80
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 8 Apr 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100