有人知道为什么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

最佳答案

我猜想在这种特殊情况下不需要进行此转换,但是编写始终执行强制转换的代码更简单,并假定SQL Server可能会对其进行优化。开发人员可能认为生成不必要的复杂SQL比维护不必要的复杂代码更好。

关于c# - Entity Framework 6.0.1不必要地强制转换smallint,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20436044/

10-13 08:16