此 ObjectContext 实例已释放,不可再用于需要连接的操作。
在Linq to Entity过程中碰到了“此 ObjectContext 实例已释放,不可再用于需要连接的操作。”其英文报错“The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.”。
在这里我想用部门员工实例讲解这个问题:
如上图所示
错误做法(一):当我们仅仅获取员工信息时候(不含部门),如果直接绑定到datagrid上时,会出现如上的问题。
分析原因,是由于导航属性Department会在datagrid中绑定,但是由于我们只是获取员工信息(不含有部门信息),导致Department导航属性出现。
修改办法:把DataGrid中的AutoGenerateColumns="True"该为False,并添加自定义模板。
代码
<DataGrid x:Name="dataGrid1" Grid.Row="1" ItemsSource="{Binding}" AutoGenerateColumns="True">
<DataGrid.Columns >
<DataGridTextColumn Header="部门编号" Binding="{Binding EmployeeID}" Width="100"/>
<DataGridTextColumn Header="部门名称" Binding="{Binding EmployeeName}" Width="100"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid.Columns >
<DataGridTextColumn Header="部门编号" Binding="{Binding EmployeeID}" Width="100"/>
<DataGridTextColumn Header="部门名称" Binding="{Binding EmployeeName}" Width="100"/>
</DataGrid.Columns>
</DataGrid>
错误做法(二):在上述基础上,如果没有使用ToList<T>()方法,也会出现这样的错误:
代码
//报错
public ObjectSet<Employee> GetEmployee()
{
using (MyDBEntities context =new MyDBEntities())
{
return context.Employees;
}
}
//报错
public IEnumerable<Employee> GetEmployee1()
{
using (MyDBEntities context =new MyDBEntities())
{
return context.Employees.AsEnumerable<Employee>();
}
}
//报错
public IQueryable<Employee> GetEmployee2()
{
using (MyDBEntities context =new MyDBEntities())
{
return context.Employees.AsQueryable<Employee>();
}
}
//通过
public List<Employee> GetEmployee3()
{
using (MyDBEntities context =new MyDBEntities())
{
return context.Employees.ToList<Employee>();
}
}
public ObjectSet<Employee> GetEmployee()
{
using (MyDBEntities context =new MyDBEntities())
{
return context.Employees;
}
}
//报错
public IEnumerable<Employee> GetEmployee1()
{
using (MyDBEntities context =new MyDBEntities())
{
return context.Employees.AsEnumerable<Employee>();
}
}
//报错
public IQueryable<Employee> GetEmployee2()
{
using (MyDBEntities context =new MyDBEntities())
{
return context.Employees.AsQueryable<Employee>();
}
}
//通过
public List<Employee> GetEmployee3()
{
using (MyDBEntities context =new MyDBEntities())
{
return context.Employees.ToList<Employee>();
}
}
如果不是通过方法调用而直接把DataGrid写在using代码块中也不会出现这样的问题的:
代码
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
using (MyDBEntities context =new MyDBEntities() )
{
this.dataGrid1.ItemsSource = context.Employees; //通过
this.dataGrid1.ItemsSource = context.Employees.AsEnumerable<Employee>(); //通过
this.dataGrid1.ItemsSource = context.Employees.AsQueryable<Employee>(); //通过
this.dataGrid1.ItemsSource = context.Employees.ToList<Employee>(); //通过
}
//this.dataGrid1.ItemsSource = GetEmployee(); //报错
//this.dataGrid1.ItemsSource = GetEmployee1(); //报错
//this.dataGrid1.ItemsSource = GetEmployee2(); //报错
//this.dataGrid1.ItemsSource = GetEmployee3(); //通过
}
{
using (MyDBEntities context =new MyDBEntities() )
{
this.dataGrid1.ItemsSource = context.Employees; //通过
this.dataGrid1.ItemsSource = context.Employees.AsEnumerable<Employee>(); //通过
this.dataGrid1.ItemsSource = context.Employees.AsQueryable<Employee>(); //通过
this.dataGrid1.ItemsSource = context.Employees.ToList<Employee>(); //通过
}
//this.dataGrid1.ItemsSource = GetEmployee(); //报错
//this.dataGrid1.ItemsSource = GetEmployee1(); //报错
//this.dataGrid1.ItemsSource = GetEmployee2(); //报错
//this.dataGrid1.ItemsSource = GetEmployee3(); //通过
}
总结:
在Linq to Entity中,尽量用自己定义的模板,如果想用AutoGenerateColumns="True",你得先关联该实体的导航属性实体,这样才能避免这样的错误!而且取数据时尽量先转换成List<T>数据集合。