我有一个非常简单的表Member
,它由以下内容组成:
CREATE TABLE [dbo].[Member](
[Member_MemberId] [int] IDENTITY(1000,1) NOT NULL,
[Member_ExternalId] [varchar](32) NULL,
[Member_ConsumerId] [varchar](32) NULL,
CONSTRAINT [PK_Member] PRIMARY KEY NONCLUSTERED
(
[Member_MemberId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
我正在使用 EF 4.0 从数据上下文运行查询,如下所示:
return Members.SingleOrDefault(member => member.ExternalId == externalId);
生成的 SQL 如下所示:
exec sp_executesql N'SELECT TOP (2)
[Extent1].[Member_MemberId] AS [Member_MemberId],
[Extent1].[Member_ExternalId] AS [Member_ExternalId],
[Extent1].[Member_ConsumerId] AS [Member_ConsumerId]
FROM [dbo].[Member] AS [Extent1]
WHERE [Extent1].[Member_ExternalId] = @p__linq__0',N'@p__linq__0 varchar(8000)',@p__linq__0='Paul'
varchar(8000)
从性能 POV 来看,此查询是次优的,因为当列本身限制为 时,它会自动将 Member_ExternalId 列强制转换为varchar(32)
。有没有办法强制 EF 生成大小与其相应行等效的动态参数?