PDA

View Full Version : سوال: ثبت چندین جدول توسط ترنزکشن



sima_2n5k
یک شنبه 08 آبان 1390, 14:10 عصر
سلام
من با استفاده از transaction سه جدول ثبت میکنم تنها مشکلی که هست اینکه ID دو جدولم در جدول دیگه باید ثبت بشه و ID ها بصورت اتوماتیک در sql2008 میخوره و تا وقتی ثبت نشه ID رو 0 میزنه و پیغام PK میگیره
چیکار میتونم بکنم؟اینم کدم:
public void InsertData()
{
DbTransaction trans = null;
using (GreenRideClassDataContext context = new GreenRideClassDataContext())
{
try
{
context.Connection.Open();
trans = context.Connection.BeginTransaction();
context.Transaction = trans;

PickupLocation pick = new PickupLocation();
pick.PicAddress = txtAddress.Text;
pick.PicZipCode = txtZip.Text;
pick.PicCtID = Convert.ToInt32(comboCity.SelectedValue);
if (Session["UserID"] != null)
pick.PicUserInfoID = Convert.ToInt32(Session["UserID"]);
else
pick.PicUserInfoID = null;

context.PickupLocations.InsertOnSubmit(pick);


DropoffLocation drop = new DropoffLocation();
drop.DropAddress = txtAddress.Text;
drop.DropZipCode = txtZip.Text;
drop.DropCtID = Convert.ToInt32(comboDCity.SelectedValue);
if (Session["UserID"] != null)
drop.DropUserInfoID = Convert.ToInt32(Session["UserID"]);
else
drop.DropUserInfoID = null;
context.DropoffLocations.InsertOnSubmit(drop);

UserHistory his = new UserHistory();
if (Session["ServiceName"] != null)
{
var dif = from d in context.Services.Where(d => d.SrvName == Session["ServiceName"]) select d;
foreach (var d in dif)
{
Session["SrvID"] = d.SrvID;
his.UHSrvID = d.SrvID;
}
}

his.UHUserInfoID = Convert.ToInt32(Session["UserID"]);
his.UHMile = 10;

his.PickupLocation = pick;
his.DropoffLocation = drop;

context.UserHistories.InsertOnSubmit(his);

context.SubmitChanges();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}

}
}