如何附加EF函数返回的xml块

时间:2015-06-08 15:53:21

标签: xml vb.net entity-framework

我有一个使用FOR XML的存储过程,它映射到ef函数。不幸的是,我发现自己因为它只返回一定长度的块而被发现。

另一个SO用户向我指出了this问题,这个问题非常诱人地表明我的问题的答案,只需附加xml的块。不幸的是,它没有继续说出如何做到这一点。任何人都可以告诉我如何做到这一点。我在vb编码。

修改

这是我的实体模型中生成的函数:

Public Overridable Function CreateErsSalesAddSubmission (ByVal uname As String, ByVal pword As String, ByVal salesContractRef As String, ByVal auctionId As String, ByVal sntype As String, ByVal action As String) As String
            Dim connection As EntityConnection = DirectCast(DirectCast(Me, IObjectContextAdapter).ObjectContext.Connection, EntityConnection)
            Dim needClose As Boolean = False
            If (connection.State <> System.Data.ConnectionState.Open) Then
              connection.Open()
              needClose = True
            End If

            Dim result As String
            Try
              Using command As EntityCommand = New EntityCommand()
                If DirectCast(Me, IObjectContextAdapter).ObjectContext.CommandTimeout.HasValue Then
                  command.CommandTimeout = DirectCast(Me, IObjectContextAdapter).ObjectContext.CommandTimeout.Value
                End If
                command.CommandType = System.Data.CommandType.StoredProcedure
                command.CommandText = "FishTrackerProfessionalEntities.CreateErsSalesAddSubmission"
                command.Connection = connection
                Dim unameParameter As EntityParameter = New EntityParameter("uname", System.Data.DbType.String)
                If (Not uname Is Nothing)
                  unameParameter.Value = uname
                End If
                command.Parameters.Add(unameParameter)
                Dim pwordParameter As EntityParameter = New EntityParameter("pword", System.Data.DbType.String)
                If (Not pword Is Nothing)
                  pwordParameter.Value = pword
                End If
                command.Parameters.Add(pwordParameter)
                Dim salesContractRefParameter As EntityParameter = New EntityParameter("salesContractRef", System.Data.DbType.String)
                If (Not salesContractRef Is Nothing)
                  salesContractRefParameter.Value = salesContractRef
                End If
                command.Parameters.Add(salesContractRefParameter)
                Dim auctionIdParameter As EntityParameter = New EntityParameter("auctionId", System.Data.DbType.String)
                If (Not auctionId Is Nothing)
                  auctionIdParameter.Value = auctionId
                End If
                command.Parameters.Add(auctionIdParameter)
                Dim sntypeParameter As EntityParameter = New EntityParameter("sntype", System.Data.DbType.String)
                If (Not sntype Is Nothing)
                  sntypeParameter.Value = sntype
                End If
                command.Parameters.Add(sntypeParameter)
                Dim actionParameter As EntityParameter = New EntityParameter("action", System.Data.DbType.String)
                If (Not action Is Nothing)
                  actionParameter.Value = action
                End If
                command.Parameters.Add(actionParameter)

                result = CType(command.ExecuteScalar(), String)
              End Using
            Finally
              If needClose Then
                connection.Close()
              End If
            End Try
            Return result
        End Function

我应该让它返回其他内容,而不是让它返回字符串的标量值(它被截断),如果是这样的话,那么如何处理返回。 感谢

1 个答案:

答案 0 :(得分:0)

我会修改你发布的功能,只需更改这些行:

Public Overridable Function CreateErsSalesAddSubmissionXml (ByVal uname As String, ByVal pword As String, ByVal salesContractRef As String, ByVal auctionId As String, ByVal sntype As String, ByVal action As String) As String
...
            using reader as EntityDataReader = command.ExecuteReader()
                If reader.HasRows Then 
                    Do While reader.Read()
                        result = result + reader.GetString(0)
                    Loop 
                End If
            End Using

用此替换行result = CType(command.ExecuteScalar(), String)

您应该重命名该方法(我在名称中添加了Xml,确保将其放在具有不同名称的文件中,以免文件被覆盖)以免您意外地超过如果你使用EF向导重新生成它,请写它。这似乎可能会为您提供正确使用相同EF连接逻辑和整体框架的最佳平衡,但仍然可以让您正确处理返回的XML - 哪些EF似乎并未真正设置(并且可能通常不需要)。