System.ObjectDisposedException:无法访问已处置的对象。对象名称:'Dispose后访问的DataContext。'。 [英] System.ObjectDisposedException: Cannot access a disposed object.Object name: 'DataContext accessed after Dispose.'.

查看:1248
本文介绍了System.ObjectDisposedException:无法访问已处置的对象。对象名称:'Dispose后访问的DataContext。'。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个使用LINQ数据上下文的可执行流程( LINQ to SQL)。出于某种原因,每当我在另一个批处理过程中执行该过程时,我都会在主题行中收到错误。这种情况发生在调用DataContext.GetTable< TENtity>()。



有没有其他人经历过这种情况?同样,当我直接运行它时,该过程有效,当我批量运行它时,但是当我尝试通过另一个.NET程序运行它时,它失败了。



谢谢。

解决方案

你能用'批处理'来解释你的意思吗?或者"分批运行"而不是通过"另一个.Net程序"运行它?



唯一一次在您的代码调用Dispose方法之后,DataContext将被释放。如果您使用C#中的'using'语句之类的构造,则可能会发生这种情况。如果您正在处理DataContext并从方法中返回查询,则在执行查询时您将收到此消息,因为DataContext已被释放。您不需要Dipose DataContext。 Dispose方法适用于某些特定场景,例如需要在应用程序状态下保留实体的副本。如果要强制DataContext为Dispose,则必须在从方法返回之前执行查询。您可以通过在查询上调用ToList()来完成此操作。




< p align = left>


Hello,

 

I have an executable process using a LINQ data context (LINQ to SQL).  For some reason, whenever I execute the process in another batch process, I'm getting the error in the subject line.  This occurs to the call DataContext.GetTable<TENtity>().

 

Has anybody else experienced this?  Again, the process works when I run it directly, and when I run it in batch, but when I try to run it through another .NET program, it fails.

 

Thanks.

解决方案

Can you explain what you mean by 'batch process' or 'run it in a batch' versus running it through 'another .Net program'?

 

The only time a DataContext becomes disposed is after your code calls the Dispose method on it. This can happen on your behalf if your using a construct like the 'using' statement in C#.  If you are disposing the DataContext and returning a query out of a method, upon execution of the query you'll get this message because the DataContext is disposed.  You don't need to Dipose the DataContext. The Dispose method is there for some specific scenarios like needing to leave a copy of an entity in application state.  If you want to force the DataContext to Dispose, you'll have to execute the query before you return it from the method.  You can do this by calling ToList() on the query.

 

 

 


这篇关于System.ObjectDisposedException:无法访问已处置的对象。对象名称:'Dispose后访问的DataContext。'。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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