开发者

Linq to SQL Int16 Gets Converted as Int32 In SQL Command

开发者 https://www.devze.com 2022-12-09 10:21 出处:网络
With the method parameter Int16? id And the Linq to SQL where clause where !id.HasValue || m.Id == id The resulting command text for the condition in the data context is

With the method parameter

Int16? id

And the Linq to SQL where clause

where !id.HasValue || m.Id == id

The resulting command text for the condition in the data context is

From the visualizer:

SELECT [t0].[Id], [t0].[Name], [t0].[IsActive]
FROM [Model] AS [t0]
WHERE (CONVERT(Int,[t0].[Id])) = @p0
-------------------------------
@p0 [Int32]: 5

My mapped class has the Id as an Int16 and the database开发者_C百科 itself has the column type as a smallint, so why does the behind the scenes sql think the parameter is an integer (Int32) and not a smallint (Int16)?


Column mapping:

    [Column(IsPrimaryKey = true, DbType="SmallInt NOT NULL", CanBeNull=false)]
    public Int16 Id { get; set; }


Change the where clause to read

where !id.HasValue || m.Id == id.Value

There is something about the nullable short that is throwing it off. I'm not sure why, but I ran into this before and found adding .Value will work.


Hmmm... I notice that you're not getting any representation of the !id.HasValue in the sql. Perhaps this is some sort of trickery related to that getting wrapped? Seems phony to me, but the only thing i can think of.

0

精彩评论

暂无评论...
验证码 换一张
取 消