Linq Query返回0结果,但应返回1

时间:2013-05-28 20:02:56

标签: c# winforms entity-framework-4 linq-to-entities sql-server-ce-3.5

我有一个用于存储订单信息的winforms应用程序的SQL Server Compact Edition版本3.5,但是我遇到了一个问题的人的投诉,该问题在细节完成后没有显示细节并提交,并通过接收用户的数据库,我可以重现问题,但我无法弄清楚为什么。我想弄清楚的是为什么他们没有同步,因为其他人工作得很好,我搜索了Stack Overflow而没有找到对我有用的答案。

对于我收到的数据库,linqpad / SSMS的结果是相同的,并且由于以下查询而产生一行(与实体框架分析器显示的查询相同):

SELECT [Extent1].[Id]                  AS [Id],
       [Extent1].[OrderHeaderId]       AS [OrderHeaderId],
       [Extent1].[Price]               AS [Price],
       [Extent1].[Quantity]            AS [Quantity],
       [Extent1].[OverridePrice]       AS [OverridePrice],
       [Extent1].[ShippingWeight]      AS [ShippingWeight],
       [Extent1].[ExtendedPrice]       AS [ExtendedPrice],
       [Extent1].[OrderId]             AS [OrderId],
       [Extent1].[ProductItemNo]       AS [ProductItemNo],
       [Extent1].[ProductItemNoTypeId] AS [ProductItemNoTypeId]
FROM   [OrderDetail] AS [Extent1]
WHERE  [Extent1].[OrderHeaderId] = 'eec06164-a052-4c23-9575-8fe1b80c8baa' /* @p__linq__0 */

但是,如果我使用以下任何一种语句,我都没有结果:

_orderDetails = ctx.OrderDetail.Where(o => o.OrderHeaderId == _order.Id).ToList();

_orderDetails = (from od in ctx.OrderDetail
                 where od.OrderHeaderId == _order.Id
                 select od).ToList();

订单标题:

-- Creating table 'OrderHeader'
CREATE TABLE [OrderHeader] (
    [Id] uniqueidentifier  NOT NULL,
    [PONumber] nvarchar(4000)  NULL,
    [InternalOrderText] nvarchar(4000)  NULL,
    [TimeStamp] datetime  NOT NULL,
    [IOCTrackingNo] nvarchar(4000)  NULL,
    [CarrierCode] nvarchar(4000)  NULL,
    [HowEntered] nvarchar(4000)  NOT NULL,
    [MessageName] nvarchar(4000)  NOT NULL,
    [RepID] nvarchar(4000)  NOT NULL,
    [BatchID] nvarchar(4000)  NOT NULL,
    [Status] nvarchar(4000)  NOT NULL,
    [ApplicationVersion] nvarchar(4000)  NOT NULL,
    [CustomerAccountID] nvarchar(4000)  NOT NULL,
    [CustomerSubAccountID] nvarchar(4000)  NOT NULL,
    [SystemArrivalDate] datetime  NULL,
    [OrderId] int  NOT NULL
);
GO

-- Creating primary key on [Id] in table 'OrderHeader'
ALTER TABLE [OrderHeader]
ADD CONSTRAINT [PK_OrderHeader]
    PRIMARY KEY ([Id] );
GO

订单明细:

-- Creating table 'OrderDetail'
CREATE TABLE [OrderDetail] (
    [Id] uniqueidentifier  NOT NULL,
    [OrderHeaderId] uniqueidentifier  NOT NULL,
    [Price] decimal(18,2)  NOT NULL,
    [Quantity] int  NOT NULL,
    [OverridePrice] bit  NOT NULL,
    [ShippingWeight] decimal(18,3)  NOT NULL,
    [ExtendedPrice] decimal(18,2)  NOT NULL,
    [OrderId] int  NOT NULL,
    [ProductItemNo] nvarchar(4000)  NOT NULL,
    [ProductItemNoTypeId] uniqueidentifier  NOT NULL
);
GO

-- Creating primary key on [Id] in table 'OrderDetail'
ALTER TABLE [OrderDetail]
ADD CONSTRAINT [PK_OrderDetail]
    PRIMARY KEY ([Id] );
GO

外键:

-- Creating foreign key on [OrderHeaderId] in table 'OrderDetail'
ALTER TABLE [OrderDetail]
ADD CONSTRAINT [FK_OrderDetailOrderHeader]
    FOREIGN KEY ([OrderHeaderId])
    REFERENCES [OrderHeader]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderDetailOrderHeader'
CREATE INDEX [IX_FK_OrderDetailOrderHeader]
ON [OrderDetail]
    ([OrderHeaderId]);
GO

1 个答案:

答案 0 :(得分:0)

我自己和其他几个人都无法弄清楚链接被破坏的原因,所以我决定将数据库更改为SQL CE 4.0因为能够使用int identity字段,我们还没有从那以后就出现了这个问题。