I have the following line of code in my EF6 (code-first) app:
var priceDetail = context.PriceLists.Include(pl => pl.PriceDetails).FirstOrDefault();
My goal is to retrieve the first PriceList object, which contains a collection of PriceDetails.
When that line executes, I receive this error:
-- Failed in 12 ms with error: Invalid column name 'Order_OrderNumber'.
This is very strange to me because neither the PriceList nor the PriceDetail objects reference the Order object. Here is the PriceList object:
[Table( "dbo.PriceList" )]
public class PriceList
{
public int PriceListId { get; set; }
[StringLength( 150 )]
[Display( Name = "PriceList name" )]
public string PriceListDesc { get; set; }
public virtual ICollection<PriceListDetail> PriceDetails { get; set; }
}
And here is the PriceDetail object:
[Table( "dbo.PriceDetail" )]
public class PriceDetail
{
[ColumnDesc( "Primary key" )]
public int PriceDetailId { get; set; }
[ForeignKey( "PriceList" )]
public int PriceListId { get; set; }
public PriceList PriceList { get; set; }
[ForeignKey( "Item" )]
public int ItemNumber { get; set; }
public Item Item { get; set; }
public Decimal Price { get; set; }
public Decimal? CostMultiplier { get; set; }
}
The full SQL query that EF generates is as follows:
SELECT
[Project1].[PriceListId] AS [PriceListId],
[Project1].[PriceListDesc] AS [PriceListDesc],
[Project1].[C1] AS [C1],
[Project1].[PriceDetailId] AS [PriceDetailId],
[Project1].[PriceListId1] AS [PriceListId1],
[Project1].[ItemNumber] AS [ItemNumber],
[Project1].[PriceList] AS [PriceList],
[Project1].[Order_OrderNumber] AS [Order_OrderNumber]
FROM ( SELECT
[Limit1].[PriceListId] AS [PriceListId],
[Limit1].[PriceListDesc] AS [PriceListDesc],
[Extent2].[PriceDetailId] AS [PriceDetailId],
[Extent2].[PriceListId] AS [PriceListId1],
[Extent2].[ItemNumber] AS [ItemNumber],
[Extent2].[PriceList] AS [PriceList],
[Extent2].[Order_OrderNumber] AS [Order_OrderNumber],
CASE WHEN ([Extent2].[PriceDetailId] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM (SELECT TOP (1) [c].[PriceListId] AS [PriceListId], [c].[PriceListDesc] AS [PriceListDesc]
FROM [Admin].[PriceList] AS [c] ) AS [Limit1]
LEFT OUTER JOIN [Admin].[PriceDetail] AS [Extent2] ON [Limit1].[PriceListId] = [Extent2].[PriceListId]
) AS [Project1]
ORDER BY [Project1].[PriceListId] ASC, [Project1].[C1] ASC
Why am I getting this error?