【发布时间】:2017-05-11 20:48:34
【问题描述】:
操作无法完成,因为 DbContext 已被释放。
我想知道是否有人帮助我解决这个问题。这是我的代码:
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 中,您知道
using() {}中的对象会自动分配,不是吗? -
做一个
C# google search on the key word SCOPE -
亲爱的@MethodMan 我明白了。我从我的代码中消除了它,错误消失了。但是在我编辑 contex info 之后,它仍然没有更新我的数据库! :(
标签: c# entity-framework objectdisposedexception