任何人都知道为什么EF6会不必要地将Int16重新投射到smallint?使用日志记录功能,我看到它执行了以下查询:
SELECT
[Project1].[LabelName] AS [LabelName], Project1.Usage, Project1.FacilityID
FROM ( SELECT
[Extent1].[LabelName] AS [LabelName],
[Extent1].[Usage] AS [Usage],
extent1.facilityid
FROM [dbo].[Label] AS [Extent1]
WHERE (0 = [Extent1].[DeleteInd])
AND ([Extent1].[FacilityID] IN (cast(1 as smallint), cast(5 as smallint)))
AND (([Extent1].[LabelName] LIKE @p__linq__0 ESCAPE '~') OR ([Extent1].[LabelName] LIKE @p__linq__1 ESCAPE '~'))
) AS [Project1]
ORDER BY [Project1].[Usage] DESC
-- p__linq__0: '%test%' (Type = AnsiString, Size = 8000)
-- p__linq__1: 'test%' (Type = AnsiString, Size = 8000)
-- Executing at 12/6/2013 4:08:49 PM -08:00
-- Completed in 16 ms with result: SqlDataReader
这是Linq查询:
Context.Database.Log = Console.Write;
var labels = (from label in Context.Labels
where label.DeleteInd == false &&
Settings.FacilitySearch.Contains(label.Facility.FacilityID) &&
(label.LabelName.Contains(searchText) || label.LabelName.StartsWith(searchText))
orderby label.Usage descending
select label.LabelName);
return labels.ToList();
EF知道FacilityId是smallint< - > Int16(会显示图片,但信誉点不够: - /)
项目: C#WinForms .NET 4.0 Windows 7 SP1 Visual Studio 2010
答案 0 :(得分:1)
我猜测在这种特殊情况下不需要这种转换,但是编写始终执行强制转换的代码更简单,并且假设SQL Server可能无论如何都会优化它。开发人员可能认为生成不必要的复杂SQL比维护不必要的复杂代码更好。