我有以下代码从表中获取一个字段
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
我无法理解为什么会这样?任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
感谢每一个人。我发现了这个问题。可能是我的错误,但我不是创建数据库所以我错过了旅游表没有主键。
我将Tour_ID字段指定为主键,并且运行良好。