Transaction on Entity FrameWork Refactoring and best performance how can i?
- by programmerist
i try to use transaction in Entity FrameWork. i have 3 tables Personel, Prim, Finans. in Prim table you look SatisTutari (int) if i add data in SatisTutari.Text instead of int value adding float value. Trannsaction must be run! Everything is ok but how can i refactoring or give best performance or best writing Transaction coding!
i have 3 table so 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),
PirimToplami 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 my tables. if you look Prim table you can see Prim value float value if i write a textbox not float value my transaction must run.
protected void btnSave_Click(object sender, EventArgs e)
{
using (TestEntities testCtx = new TestEntities())
{
using (TransactionScope scope = new TransactionScope())
{
Personel personel = new Personel();
Prim prim = new Prim();
Finans finans = new Finans();
//-----------------------------------------------------------------------Step 1
personel.Ad = txtName.Text;
personel.Soyad = txtSurName.Text;
personel.Meslek = txtMeslek.Text;
personel.DogumTarihi = DateTime.Parse(txtSatisTarihi.Text);
personel.DogumYeri = txtDogumYeri.Text;
personel.PirimToplami = float.Parse(txtPrimToplami.Text);
testCtx.AddToPersonel(personel);
testCtx.SaveChanges();
//----------------------------------------------------------------------- step 2
prim.PersonelID = personel.PersonelID;
prim.SatisTutari = int.Parse(txtSatisTutari.Text);
prim.SatisTarihi = DateTime.Parse(txtSatisTarihi.Text);
prim.Prim1 = double.Parse(txtPrim.Text);
finans.Tutar = prim.SatisTutari * prim.Prim1;
testCtx.AddToPrim(prim);
testCtx.SaveChanges();
//----------------------------------------------------------------------- step 3
lblTutar.Text = finans.Tutar.Value.ToString();
testCtx.AddToFinans(finans);
testCtx.SaveChanges();
scope.Complete();
}
}
How can i rearrange codes. i need best practice refactoring and best solution for reading easly and performance!!!