PDA

View Full Version : سوال: مشکل با حذف چند رکورد به شرط یکی بودن ای دی کلید با entity



Fartaj
جمعه 22 فروردین 1393, 10:36 صبح
سلام خدمت دوستان
من می خوام از جدول ارتباطی رکوردهایی که فیلد آی دی ارتباطی مقدارش برابر باشه با اونی که کاربر انتخاب کرده حذف کنم اما فقط یکی رو حذف می کنه



var qsearch = db.inquiryjoin.Where(i => i.InquiryId == a);
foreach (var item1 in qsearch)
{


inquiryjoin inqj = db.inquiryjoin.First(i => i.InquiryId == item1.InquiryId);
db.inquiryjoin.DeleteObject(inqj);

}
db.SaveChanges();
gridInquiry.DataSource = db.inquiries.ToList();

مهرداد صفا
جمعه 22 فروردین 1393, 13:13 عصر
سلام.
این خط:


db.SaveChanges(); رو وارد بلاک for کرده و مجددا امتحان کنید.

Fartaj
جمعه 22 فروردین 1393, 14:45 عصر
متعصفانه خطا می ده


System.Data.EntityException was unhandled
Message=An error occurred while starting a transaction on the provider connection. See the inner exception for details.
Source=System.Data.Entity
StackTrace:
at System.Data.EntityClient.EntityConnection.BeginDbT ransaction(IsolationLevel isolationLevel)
at System.Data.EntityClient.EntityConnection.BeginTra nsaction()
at System.Data.Objects.ObjectContext.SaveChanges(Save Options options)
at System.Data.Objects.ObjectContext.SaveChanges()
at taban.frmInquiry.btnDelete_Click(Object sender, EventArgs e) in E:\Programming\APPLICATION\C#‎‎‎‎‎‎‎ \taban\New folder\taban\taban\frmInquiry.cs:line 104
at System.Windows.Forms.Control.OnClick(EventArgs e)
at Telerik.WinControls.RadControl.OnClick(EventArgs e)
at Telerik.WinControls.UI.RadButtonBase.ButtonElement _Click(Object sender, EventArgs e)
at Telerik.WinControls.RadItem.OnClick(EventArgs e)
at Telerik.WinControls.UI.RadButtonItem.OnClick(Event Args e)
at Telerik.WinControls.RadItem.DoClick(EventArgs e)
at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadEl ement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseRoutedEvent(Ra dElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadEl ement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseRoutedEvent(Ra dElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.DoMouseUp(MouseEven tArgs e)
at Telerik.WinControls.ComponentInputBehavior.OnMouse Up(MouseEventArgs e)
at Telerik.WinControls.RadControl.OnMouseUp(MouseEven tArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at Telerik.WinControls.RadControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at taban.frmMain.MenuInquiry_Click(Object sender, EventArgs e) in E:\Programming\APPLICATION\C#‎‎‎‎‎‎‎ \taban\New folder\taban\taban\frmMain.cs:line 99
at Telerik.WinControls.RadItem.OnClick(EventArgs e)
at Telerik.WinControls.UI.RadButtonItem.OnClick(Event Args e)
at Telerik.WinControls.UI.RadMenuItem.OnClick(EventAr gs e)
at Telerik.WinControls.RadItem.DoClick(EventArgs e)
at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadEl ement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseRoutedEvent(Ra dElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadEl ement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseRoutedEvent(Ra dElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.DoMouseDown(MouseEv entArgs e)
at Telerik.WinControls.ComponentInputBehavior.OnMouse Down(MouseEventArgs e)
at Telerik.WinControls.RadControl.OnMouseDown(MouseEv entArgs e)
at Telerik.WinControls.UI.RadMenu.OnMouseDown(MouseEv entArgs e)
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at Telerik.WinControls.RadControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at taban.Splash.timer1_Tick(Object sender, EventArgs e) in E:\Programming\APPLICATION\C#‎‎‎‎‎‎‎ \taban\New folder\taban\taban\Splash.cs:line 51
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndPr oc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at taban.Program.Main() in E:\Programming\APPLICATION\C#‎‎‎‎‎‎‎ \taban\New folder\taban\taban\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Data.SqlClient.SqlException
Message=New transaction is not allowed because there are other threads running in the session.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=3988
Procedure=""
Server=.
State=1
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.TdsExecuteTransact ionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalConnectionTds.Exe cuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalConnectionTds.Exe cuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalConnection.BeginS qlTransaction(IsolationLevel iso, String transactionName)
at System.Data.SqlClient.SqlInternalConnection.BeginT ransaction(IsolationLevel iso)
at System.Data.SqlClient.SqlConnection.BeginDbTransac tion(IsolationLevel isolationLevel)
at System.Data.Common.DbConnection.BeginTransaction(I solationLevel isolationLevel)
at System.Data.EntityClient.EntityConnection.BeginDbT ransaction(IsolationLevel isolationLevel)
InnerException:






این کل کد حذف من :


private void btnDelete_Click(object sender, EventArgs e)
{
Janus.Windows.GridEX.GridEXRow[] CheckedRows;
CheckedRows = this.gridInquiry.GetCheckedRows();
if ((CheckedRows.Length == 0))
{
PersianMessageBox.Show("موردی برای حذف انتخاب نشده است !", "خطا", PersianMessageBox.Buttons.OK, PersianMessageBox.Icon.Error);
return;
}
else
{

if (PersianMessageBox.Show("آیا مطمئن هستید؟", "حذف استعلام", PersianMessageBox.Buttons.YesNo, PersianMessageBox.Icon.Question, PersianMessageBox.DefaultButton.Button2) == DialogResult.Yes)
{

foreach (Janus.Windows.GridEX.GridEXRow row in CheckedRows)
{
int a = Convert.ToInt32(row.Cells["InquiryId"].Value);

var qsearch = db.inquiryjoin.Where(i => i.InquiryId == a);


foreach (var item in qsearch)
{
int typeid = Convert.ToInt32(item.TypeId);
int tid = Convert.ToInt32(item.TId);

switch (typeid)
{

case 0:
compact cmp = db.compacts.First(i => i.CompactId == tid);
db.compacts.DeleteObject(cmp);
break;

case 1:
traditional td = db.traditionals.First(i => i.TraditionalId == tid);
db.traditionals.DeleteObject(td);
break;

case 2:
air ai = db.airs.First(i => i.AirId == tid);
db.airs.DeleteObject(ai);
break;
}
}


//delete from relational table
foreach (var item1 in qsearch)
{
inquiryjoin inqj = db.inquiryjoin.First(i => i.InquiryId == item1.InquiryId);
db.inquiryjoin.DeleteObject(inqj);
}

//delete from main table
inquiry inq = db.inquiries.First(i => i.InquiryId == a);
db.inquiries.DeleteObject(inq);
}

db.SaveChanges();
gridInquiry.DataSource = db.inquiries.ToList();

}

}

}
}

Fartaj
جمعه 22 فروردین 1393, 14:54 عصر
متعصفانه خطا می ده

Mahmoud.Afrad
جمعه 22 فروردین 1393, 15:07 عصر
اشکال در استفاده از متد First هست. چرا دوباره آیتم ها رو مورد جستجو قرار میدی؟!!!
به اینصورت بنویس:

var qsearch = db.inquiryjoin.Where(i => i.InquiryId == a);
foreach (inquiryjoin item1 in qsearch)
{
db.inquiryjoin.DeleteObject(item1);
}
db.SaveChanges();
gridInquiry.DataSource = db.inquiries.ToList();