操作无法完成,因为 DbContext 已被释放。影响运行个程序
The operation cannot be completed because the DbContext has been disposed. Affect running program
The operation cannot be completed because the DbContext has been disposed.
我想知道是否有人可以帮助我解决这个问题。这是我的代码:
public partial class CustomerResearchForm : MetroFramework.Forms.MetroForm
{
FactorEntities contex;
public CustomerResearchForm()
{
InitializeComponent();
}
private void CustomerResearchForm_Load(object sender, EventArgs e)
{
}
private void CResearchGrid_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
CustomerUpdateAndDelete CustomerUpdateAndDelete = new CustomerUpdateAndDelete();
using ( contex =new FactorEntities())
{
var sendergrid=(DataGridView)sender;
int customercode = Convert.ToInt32(sendergrid.Rows[e.RowIndex].Cells[1].Value);
var customer = from _customer in contex.tblCustomers where
_customer.CustomerCode==customercode select _customer;
CustomerUpdateAndDelete.tblCustomerBindingSource.DataSource = customer.ToList();
CustomerUpdateAndDelete.Show();
CustomerUpdateAndDelete.tblCustomerBindingNavigatorSaveItem.Click+=tblCustomerBindingNavigatorSaveItem_Click;
}
}
private void tblCustomerBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
contex.SaveChanges();
throw new NotImplementedException();
}
}
异常发生在这一行:
contex.SaveChanges();
我不能在上下文中使用 var,我该怎么办?
您的 using
语句将在 CResearchGrid_CellMouseDoubleClick()
中的右括号 }
的末尾自动处理 contex
我不太确定你在保存什么,但是你应该添加一个 using
语句并初始化你的 contex
对象。如果您确实要在每个方法中初始化 contex
,则应将其从 class 成员声明中删除。请注意,您需要修改您的实体或将新实体添加到您的 contex
对象才能真正保存任何内容。没有它,您实际上并没有节省任何东西。
另一种方法是在构造函数中初始化 contex
并实现 IDisposable
。然后就可以在Dispose()
方法中调用contex.Dispose()
了
The operation cannot be completed because the DbContext has been disposed.
我想知道是否有人可以帮助我解决这个问题。这是我的代码:
public partial class CustomerResearchForm : MetroFramework.Forms.MetroForm
{
FactorEntities contex;
public CustomerResearchForm()
{
InitializeComponent();
}
private void CustomerResearchForm_Load(object sender, EventArgs e)
{
}
private void CResearchGrid_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
CustomerUpdateAndDelete CustomerUpdateAndDelete = new CustomerUpdateAndDelete();
using ( contex =new FactorEntities())
{
var sendergrid=(DataGridView)sender;
int customercode = Convert.ToInt32(sendergrid.Rows[e.RowIndex].Cells[1].Value);
var customer = from _customer in contex.tblCustomers where
_customer.CustomerCode==customercode select _customer;
CustomerUpdateAndDelete.tblCustomerBindingSource.DataSource = customer.ToList();
CustomerUpdateAndDelete.Show();
CustomerUpdateAndDelete.tblCustomerBindingNavigatorSaveItem.Click+=tblCustomerBindingNavigatorSaveItem_Click;
}
}
private void tblCustomerBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
contex.SaveChanges();
throw new NotImplementedException();
}
}
异常发生在这一行:
contex.SaveChanges();
我不能在上下文中使用 var,我该怎么办?
您的 using
语句将在 CResearchGrid_CellMouseDoubleClick()
}
的末尾自动处理 contex
我不太确定你在保存什么,但是你应该添加一个 using
语句并初始化你的 contex
对象。如果您确实要在每个方法中初始化 contex
,则应将其从 class 成员声明中删除。请注意,您需要修改您的实体或将新实体添加到您的 contex
对象才能真正保存任何内容。没有它,您实际上并没有节省任何东西。
另一种方法是在构造函数中初始化 contex
并实现 IDisposable
。然后就可以在Dispose()
方法中调用contex.Dispose()
了