I’m using Code First and am trying to call a stored procedure and have it map to one of my classes. I created a stored procedure, BOMComponentChild, that returns details of a Component with information of its hierarchy in PartsPath and MyPath. I have a class for the output of this stored procedure. I’m having an issue where everything except the two columns, PartsPath and MyPath, are being mapped correctly with these two properties ending up as Nothing. I searched around and from my understanding the mapping bypasses any Entity Framework name mapping and uses column name to property name. The names are the same and I’m not sure why it is only these two columns.
The last part of the stored procedure is:
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
and the class it maps to is:
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
I am trying to call it like this:
Me.Database.SqlQuery(Of BOMComponentChild)("EXEC [BOMComponentChild] @ComponentID, @PathPrefix, @IsAssembly", params).ToList()
When I run the stored procedure in management studio, the columns are correct and not null. I just can’t figure out why these won’t map as they are the important information in the stored procedure. The types for PartsPath and MyPath are varchar(50).
EDIT
So I realized the only difference between those columns and the other ones is that they are made with casts. I replaced them in the end select with a static string and they were mapped correctly. When i replaced the last select with the ‘test’ i get the values are mapped. What is wrong with my CASTs if they are displayed correctly on in a SQL query.
;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
...
In my infinite wisdom I had the parameter PathPrefix default to ‘/’ in my query, however, I was passing in PathPrefix as DBNull, which is not the same as not passing in a value. So the NULL propagated through the query.
Sorry if I wasted anyone’s time.