I try to use transactions 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. Transaction 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),
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 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.PirimToplamı = 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 easily and performance!!!