1

我正在使用 Code First 并尝试调用存储过程并将其映射到我的一个类。我创建了一个存储过程 BOMComponentChild,它返回组件的详细信息以及 PartsPath 和 MyPath 中的层次结构信息。我有一个用于输出此存储过程的类。我遇到了一个问题,除了两列 PartsPath 和 MyPath 之外的所有内容都被正确映射,这两个属性最终为 Nothing。我四处搜索,根据我的理解,映射绕过任何实体框架名称映射,并使用列名到属性名。名称相同,我不确定为什么只有这两列。

存储过程的最后一部分是:

SELECT   t.ParentID
        ,t.ComponentID
        ,c.PartNumber
        ,t.PartsPath
        ,t.MyPath
        ,t.Layer
        ,c.[Description]
        ,loc.LocationID
        ,loc.LocationName
        ,CASE WHEN sup.SupplierID IS NULL THEN 1 ELSE sup.SupplierID END AS SupplierID
        ,CASE WHEN sup.SupplierName IS NULL THEN 'Default' ELSE sup.SupplierName END AS SupplierName
        ,c.Active
        ,c.QA
        ,c.IsAssembly
        ,c.IsPurchasable
        ,c.IsMachined
        ,t.QtyRequired
        ,t.TotalQty

FROM    BuildProducts                       t 
        INNER JOIN [dbo].[BOMComponent]     c       ON c.ComponentID    = t.ComponentID         
        LEFT JOIN [dbo].[BOMSupplier]       bsup    ON bsup.ComponentID = t.ComponentID AND bsup.IsDefault = 1
        LEFT JOIN [dbo].[LookupSupplier]    sup     ON sup.SupplierID   = bsup.SupplierID
        LEFT  JOIN [dbo].[LookupLocation]   loc     ON loc.LocationID   = c.LocationID
WHERE 
        (@IsAssembly IS NULL OR IsAssembly = @IsAssembly)
ORDER BY
         t.MyPath

它映射到的类是:

Public Class BOMComponentChild

        Public Property ParentID As Nullable(Of Integer)
        Public Property ComponentID As Integer
        Public Property PartNumber As String
        Public Property MyPath As String
        Public Property PartsPath As String
        Public Property Layer As Integer
        Public Property Description As String
        Public Property LocationID As Integer
        Public Property LocationName As String
        Public Property SupplierID As Integer
        Public Property SupplierName As String
        Public Property Active As Boolean
        Public Property QA As Boolean
        Public Property IsAssembly As Boolean
        Public Property IsPurchasable As Boolean
        Public Property IsMachined As Boolean
        Public Property QtyRequired As Integer
        Public Property TotalQty As Integer

        Public Property Children As IDictionary(Of String, BOMComponentChild) = New Dictionary(Of String, BOMComponentChild)

    End Class

我试图这样称呼它:

Me.Database.SqlQuery(Of BOMComponentChild)("EXEC [BOMComponentChild] @ComponentID, @PathPrefix, @IsAssembly", params).ToList()

当我在管理工作室中运行存储过程时,这些列是正确的而不是空的。我只是无法弄清楚为什么这些不会映射,因为它们是存储过程中的重要信息。PartsPath 和 MyPath 的类型是 varchar(50)。

编辑

所以我意识到这些列和其他列之间的唯一区别是它们是用铸件制成的。我最后用静态字符串替换了它们,它们被正确映射。当我用“测试”替换最后一个选择时,我得到了映射的值。如果我的 CAST 在 SQL 查询中正确显示,它们有什么问题。

;WITH BuildProducts ( ParentID, ComponentID, PartNumber
                    ,PartsPath, MyPath, QtyRequired, TotalQty, Layer)
AS
(
    SELECT   ...
            ,@PathPrefix                        AS PartsPath
            ,CAST(@PathPrefix +  
                    CAST(ComponentID            AS varchar(4))  +
                    '/'                         AS varchar(50)
                  )                             AS MyPath
            ,... 
    FROM    [dbo].[BOMComponent]            
    WHERE   ...

    UNION ALL

    SELECT   ...
            ,CAST(b.PartsPath + 
                    CAST(a.ParentID             AS varchar(4))  +
                    '/'                         AS varchar(50)
                  )                             AS PartsPath
            ,CAST(b.PartsPath + 
                    CAST(a.ParentID             AS varchar(4))  +
                    '/'                                         +
                    CAST(a.ComponentID          AS varchar(4))  +
                    '/'                         AS varchar(50)
                  )                             AS MyPath
            ,...
    FROM    [dbo].[BOMAssembly]             a 
            INNER JOIN [dbo].[BOMComponent] c ON a.ComponentID = c.ComponentID
            ...
)

SELECT  -- t.PartsPath
        --,t.MyPath
        'test' As PartsPath
        ,'test' As MyPath
        ,...

FROM    BuildProducts                       t 
        ...
4

1 回答 1

0

在我无限的智慧中,我在查询中将参数 PathPrefix 默认为“/”,但是,我将 PathPrefix 作为 DBNull 传递,这与不传递值不同。因此 NULL 通过查询传播。

对不起,如果我浪费了任何人的时间。

于 2012-10-31T14:50:40.787 回答