实体框架一对多关系父对象在子节点中为空,而父节点已设置

时间:2014-01-22 08:26:55

标签: vb.net entity-framework

我遇到EF的问题,我似乎无法解决。就像标题所说,我有一对多关系。当我想从孩子那里检索父对象时,我得到Nothing(vb.net);

  

对象引用未设置为对象的实例。

,而在父对象中设置了parentobjectID。

这是我到目前为止所尝试的内容:

  1. 关闭延迟装载,没有帮助。
  2. 检查外键在数据库中设置正确10次。
  3. 如果父子项的多样性,请检入edmx模型 也是正确的10次。
  4. 试图通过创建上下文来欺骗部分类 通过parentID检索正确的父对象。哪个给了我 另一个有意义的错误;

      

    无法定义两个对象之间的关系,因为   它们附加到不同的ObjectContext对象。

  5. 我需要说这个孩子有另一个设置正确的父母。

    Code Auto生成的类TestStepPageElement

    Imports System
    Imports System.Collections.Generic
    
    Partial Public Class TestStepPageElement
        Public Property TestStepPageElementID As System.Guid
        Public Property PageElementID As System.Guid
        Public Property TestStepID As System.Guid
        Public Property Order As Integer
    
        Public Overridable Property PageElement As PageElement
        Public Overridable Property TestStep As TestStep
        Public Overridable Property TestStepPageElementParameters As ICollection(Of TestStepPageElementParameter) = New HashSet(Of TestStepPageElementParameter)
    
    End Class
    

    自动生成代码父

    Imports System
        Imports System.Collections.Generic
    
        Partial Public Class PageElement
            Public Property PageElementID As System.Guid
            Public Property FriendlyName As String
            Public Property Type As TestMonkey.Shared.Enumerations.PageElementType
            Public Property PageID As System.Guid
            Public Property Description As String
            Public Property IdentifierType As TestMonkey.Shared.Enumerations.PageElementIdentifierType
            Public Property IdentifierOptions As String
            Public Property IsDeleted As Boolean
    
            Public Overridable Property Page As Page
            Public Overridable Property TestStepPageElements As ICollection(Of TestStepPageElement) = New HashSet(Of TestStepPageElement)
    
        End Class
    

    这是TestStepPageElement的部分类,其中Me.PageElement是什么

        Partial Public Class TestStepPageElement
    
        ''' <summary>Converts the object to info</summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function ToTestStepPageElementInfo() As [Shared].TestStepPageElementInfo
    
            Dim objInfo As New [Shared].TestStepPageElementInfo() With
                {
                    .TestStepPageElementID = Me.TestStepPageElementID,
                    .TestStepID = Me.TestStepID,
                    .PageID = Me.PageElement.PageID,
                    .PageElementID = Me.PageElementID,
                    .PageElementFriendlyName = Me.PageElement.FriendlyName,
                    .PageElementType = Me.PageElement.Type,
                    .PageElementIdentifierType = Me.PageElement.IdentifierType,
                    .PageElementIdentifierOptions = Me.PageElement.IdentifierOptions,
                    .Order = Me.Order,
                    .VariableNames = (From tspep In Me.TestStepPageElementParameters
                                      Order By tspep.Order
                                      Select tspep.VariableName).ToArray()
                }
    
            Return objInfo
    
        End Function
    
    End Class
    
    Imports System.Linq
    Imports System.Text.RegularExpressions
    Imports System.Text
    Imports System.Reflection
    

    这是这个部分类的名称

    ''' <summary></summary>
    ''' <remarks></remarks>
    Partial Class TestStep
    
        ''' <summary>Converts the test step to info</summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function ToTestStepInfo() As [Shared].TestStepInfo
    
            Dim objInfo As New [Shared].TestStepInfo() With
                {
                    .TestStepID = Me.TestStepID,
                    .FullStepText = Me.FullStepText,
                    .Order = Me.Order,
                    .PageID = Me.PageID,
                    .ReferencedTestID = Me.ReferencedTestID,
                    .ReferencedTestParameters = Me.ReferencedTestParameter.Select(Function(rtp) rtp.ToReferencedTestParameterInfo()).ToArray(),
                    .Type = Me.Type,
                    .TestStepSetDimensions = Me.TestStepSetDimensions.Select(Function(tssd) tssd.ToTestStepSetDimensionInfo()).ToArray(),
                    .TestStepPageElements = (From tspe In Me.TestStepPageElements
                                            Order By tspe.Order
                                            Select tspe.ToTestStepPageElementInfo()).ToArray()
                }
    
            Return objInfo
        End Function
    
    End Class
    

1 个答案:

答案 0 :(得分:0)

我很抱歉,我觉得很蠢。它似乎正在创建一个没有PageElement属性值的新对象。感谢您的帮助!