ObjectContext实例已被释放,不能再用于需要连接的操作' [英] The ObjectContext instance has been disposed and can no longer be used for operation that require connection'

查看:40
本文介绍了ObjectContext实例已被释放,不能再用于需要连接的操作'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



最近我开始学习实体框架和Linq to Entities。目前,我正在努力应对以下Linq Expession给我的错误消息如下:



System.Reflection.TargetInvocationException:Properti accessor''Customer''on on对象''ITOrder.Model.Order''引发了以下异常:ObjectContext实例已被处理,不能再用于需要连接的操作''



Linq表达:

 使用(OrderITEntities context =  new  OrderITEntities())
{
var result =( from p context.Orders
其中 p.Customer.Name == Billl Clay
选择 p).ToList();

dataGridView1.DataSource = result;





我也尝试了下面的Linq但是抛出了同样的错误:

  var  result = 来自 p  in  context.Orders 
其中 p.Customer.Name == Billl Clay
select p;

dataGridView1.DataSource = result;





请帮我理解错误以及如何制作tis工作正常。

谢谢A.

解决方案

如果您尝试从Order对象访问相关属性,则可能有可能

Ex。如果您尝试在Grid Binding中从Order对象访问Customer信息,那么它可能会抛出此错误。

如果您正在使用cutomer信息,请尝试在结果数据中提前加载它。



尝试以下代码

列表< order>结果; 
使用(OrderITEntities context = new OrderITEntities())
{
result =( from p in context.Orders.Include(' 客户'
其中 p.Customer.Name == < span class =code-string> Billl Clay
选择 p)的.ToList();
}
dataGridView1.DataSource = result;
dataGridView1.DataBind();
< / 订单 >


Hi,
recentlly I started to learn Entity Framework and Linq to Entities. Currentlly, I''m struggling with the folowing Linq Expession returning me the errorm message as follows:

System.Reflection.TargetInvocationException: Properti accessor ''Customer'' on object ''ITOrder.Model.Order'' threw the following exception: The ObjectContext instance has been disposed and can no longer be used for operation that require connection''...

The Linq expresions:

using (OrderITEntities context = new OrderITEntities())
           {
                        var result = (from p in context.Orders
                             where p.Customer.Name == "Billl Clay"
                            select p).ToList();

               dataGridView1.DataSource = result;



also I tried the following Linq but the same error is thrown:

var result = from p in context.Orders
                              where p.Customer.Name == "Billl Clay"
                             select p;

                dataGridView1.DataSource = result;



Please help me to understand what is wrong and how to make tis working properly.
Thanks A.

解决方案

It might be possible if you are trying to access related properties from Order object
Ex. If you are trying to access Customer information from Order object while Grid Binding then it might throw this error.
If you are using cutomer information then Try Early Loading of it in resulting data.

Try Below code

List<order> result; 
 using (OrderITEntities context = new OrderITEntities())
            {
                         result = (from p in context.Orders.Include('Customers')
                              where p.Customer.Name == "Billl Clay"
                             select p).ToList();
            }
                dataGridView1.DataSource = result;
                dataGridView1.DataBind();
</order>


这篇关于ObjectContext实例已被释放,不能再用于需要连接的操作'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆