Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net DataContext处理错误已解决,但原因是什么?_Asp.net_Vb.net - Fatal编程技术网

Asp.net DataContext处理错误已解决,但原因是什么?

Asp.net DataContext处理错误已解决,但原因是什么?,asp.net,vb.net,Asp.net,Vb.net,我正在使用VB.Net。我已经解决了一个行为,其中代码返回错误“无法访问已处置对象。对象名称:'DataContext access after Dispose.” 我的问题是:我根本不明白它为什么有效! 以下是发生错误时的代码: Public Function Element_Import(ByVal id_element As Integer) As elements Using db As New GlobalDataContext(MyConnexion) R

我正在使用VB.Net。我已经解决了一个行为,其中代码返回错误“无法访问已处置对象。对象名称:'DataContext access after Dispose.” 我的问题是:我根本不明白它为什么有效! 以下是发生错误时的代码:

Public Function Element_Import(ByVal id_element As Integer) As elements

    Using db As New GlobalDataContext(MyConnexion)

        Return (From element In db.elements
                Select element
                Where element.id_element = id_element).First
    End Using

End Function
然后我尝试重新获取数据:

Dim myElement As elements = _MyConnection.Element_Import(id_element)
Dim myLocal As List(Of elements_localisation) = myElement.elements_localisation.ToList '-- ObjectDisposedException !
Dim myElement As elements = _MyConnection.Element_ImportContext(id_element)
Dim myLocal As List(Of elements_localisation) = myElement.elements_localisation.ToList '-- IT WORKS !
当我导入元素,然后尝试访问名为“元素本地化”和“元素文件”的子表时,会发生ObjectDisposedException。这是公平的,因为DataContext不可用

所以我做了一些不同的事情:

 Public Function Element_ImportContext(ByVal id_element As Integer) As elements
    Dim myElement As elements
    Dim myElementLocalisation As New List(Of elements_localisation)
    Dim myElementFiles As New List(Of elements_files)
    Using db As New GlobalDataContext(MyConnexion)
        myElement = (From element In db.elements
                Select element
                Where element.id_element = id_element).First

        myElementLocalisation = myElement.elements_localisation.ToList
        myElementFiles = myElement.elements_files.ToList
    End Using
    Return myElement

End Function
然后我尝试重新获取数据:

Dim myElement As elements = _MyConnection.Element_Import(id_element)
Dim myLocal As List(Of elements_localisation) = myElement.elements_localisation.ToList '-- ObjectDisposedException !
Dim myElement As elements = _MyConnection.Element_ImportContext(id_element)
Dim myLocal As List(Of elements_localisation) = myElement.elements_localisation.ToList '-- IT WORKS !
我真的很想了解发生了什么,因为在这两种情况下,DataContext都被处理了


有人能解释一下吗?

当您编写LINQ时,您编写了一个针对某些数据执行的查询,该查询仅在代码的另一部分需要数据时执行

在本例中,您返回的是
myElement
变量,因为您使用了
.First()
,这将强制执行查询并返回第一项

属性
elements\u本地化
elements\u文件
可能是虚拟属性,仅在您请求时加载。(实体框架就是这样,我不确定您在这里使用的是什么)

您的程序将尝试访问虚拟财产,然后虚拟财产将转到数据上下文以获取您请求的下一位数据,但在您的情况下,数据上下文将被释放


您的第二种方法之所以有效,是因为您在虚拟属性上使用了
ToList()
,这会强制在处理数据上下文之前随时检索数据。

谢谢您的回答。您的意思是,通过使用ToList使数据像克隆行为一样持久化?我偶然发现了此解决方案,因为我首先尝试执行以下操作:…\n MyelementLocalization=myElement.elements\u Localization\n myElementFiles=myElement.elements\u文件\n使用\n myElement.elements\u Localization=MyelementLocalization\n myElement.elements\u文件结束myElementFiles\n返回myElement\n结束函数\n\n但以相同的方式失败。。。