2014-01-07 63 views
3

我有下面的代码,用于从表实体框架生成不同的SQL查询

var a = db.Customs 
      .Where(x => x.Cust_ID == tid) 
      .Select(x => x.TransferCommission.Value) 
      .FirstOrDefault().ToString(); 

获得一个场,该代码生成低于SQL查询

SELECT TOP (1) 
[Extent1].[TransferCommission] AS [TransferCommission] 
FROM [dbo].[Custom] AS [Extent1] 
WHERE 54612 = [Extent1].[Cust_ID] 

但是这个代码

var b = db.Tours 
      .Where(x => x.Tour_ID == tid) 
      .Select(x => x.Supl_ID.Value) 
      .FirstOrDefault() 
      .ToString(); 

生成以下SQL查询

exec sp_executesql N'SELECT 
[Limit1].[Supl_ID] AS [Supl_ID] 
FROM (SELECT TOP (1) 
    [Extent1].[Supl_ID] AS [Supl_ID] 
    FROM (SELECT 
[Toursn].[Airline_ID] AS [Airline_ID], 
[Toursn].[Tour_Code] AS [Tour_Code], 
[Toursn].[Supl_ID] AS [Supl_ID], 
[Toursn].[Start_Destination_ID] AS [Start_Destination_ID], 
[Toursn].[End_Destination_ID] AS [End_Destination_ID], 
[Toursn].[Tour_Name] AS [Tour_Name], 
[Toursn].[Service_Category_ID] AS [Service_Category_ID], 
[Toursn].[Tour_Option_Name] AS [Tour_Option_Name], 
[Toursn].[Run_MinPax] AS [Run_MinPax], 
[Toursn].[Notes] AS [Notes], 
[Toursn].[Advse_Book] AS [Advse_Book], 
[Toursn].[Tour_Desc] AS [Tour_Desc], 
[Toursn].[Tour_Duration] AS [Tour_Duration], 
[Toursn].[Distance] AS [Distance], 
[Toursn].[Tour_ID] AS [Tour_ID], 
[Toursn].[Tour_Map1] AS [Tour_Map1], 
[Toursn].[Tour_Num_Days_Cruise] AS [Tour_Num_Days_Cruise], 
[Toursn].[Tour_Itinerary_File] AS [Tour_Itinerary_File], 
[Toursn].[Cruise_Supl_ID] AS [Cruise_Supl_ID], 
[Toursn].[Toursn_Deleted] AS [Toursn_Deleted], 
[Toursn].[Tour_Picture] AS [Tour_Picture], 
[Toursn].[motherTour_ID] AS [motherTour_ID], 
[Toursn].[isMotherTour] AS [isMotherTour], 
[Toursn].[Tour_cruise_before_day] AS [Tour_cruise_before_day], 
[Toursn].[Tour_Thumbnail] AS [Tour_Thumbnail], 
[Toursn].[MainNotification] AS [MainNotification], 
[Toursn].[Operated_Supl_ID] AS [Operated_Supl_ID], 
[Toursn].[TransferStatus] AS [TransferStatus], 
[Toursn].[Escor] AS [Escor], 
[Toursn].[OnList] AS [OnList], 
[Toursn].[PicUploadDate] AS [PicUploadDate], 
[Toursn].[TransCat] AS [TransCat], 
[Toursn].[TransferNo] AS [TransferNo], 
[Toursn].[TransferDuration] AS [TransferDuration], 
[Toursn].[DeparTime] AS [DeparTime], 
[Toursn].[ArrivTime] AS [ArrivTime], 
[Toursn].[Educational] AS [Educational], 
[Toursn].[Tour_Picture_AltTag] AS [Tour_Picture_AltTag], 
[Toursn].[OnlyBookingAgent] AS [OnlyBookingAgent], 
[Toursn].[DuplicateDate] AS [DuplicateDate], 
[Toursn].[Duplicate] AS [Duplicate], 
[Toursn].[DuplicateNo] AS [DuplicateNo], 
[Toursn].[Toursn_CruiseCnt] AS [Toursn_CruiseCnt] 
FROM [dbo].[Toursn] AS [Toursn]) AS [Extent1] 
    WHERE ([Extent1].[Tour_ID] = @p__linq__0) AND (@p__linq__0 IS NOT NULL) 
) AS [Limit1]',N'@p__linq__0 int',@p__linq__0=25 

我不明白为什么会发生?任何人都可以帮助我解决这个问题吗?

+1

其在'Toursn'表格中的字段数目使其看起来很戏剧化。但是,如果将这些消除掉,则只剩下几乎相同的查询。只是简单子查询的前1个,而不是表格的前1个。 – paqogomez

+1

“Cust_ID”和“Tour_ID”列的数据库和EF类型是什么? – MarcinJuraszek

回答

0

感谢每一个。我发现了这个问题。可能是我的错误,但我不是创建数据库,所以我错过了游览表没有主键。

我将Tour_ID字段分配为主键,它的工作很好。