Why does Entity Framework generate large parameters? How can they be reduced?

房东的猫 提交于 2020-01-04 05:19:11

问题


In a very simple db query:

_service.GetAll<Visitor>().Any(r => r.EmailAddress == email)

when tracing this using Glimpse, it shows a parameter @p__linq__0 of Type String and Size 4000 being passed to the database. The following SQL is generated:

SELECT
CASE WHEN ( EXISTS (SELECT
    1 AS [C1]
    FROM  [dbo].[Visitor] AS [Extent1]
    INNER JOIN [dbo].[VisitorType] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]
    WHERE ([Extent2].[EmailAddress] = 'test@test.com' /* @p__linq__0 */) OR (([Extent2].[EmailAddress] IS NULL) AND ('test@test.com' /* @p__linq__0 */ IS NULL))
)) THEN cast(1 as bit) ELSE cast(0 as bit) END AS [C1]
FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]

This seems quite wasteful, when a MaxLength of 254 has been defined.

Can anyone explain why this is? And how I can change the parameter size?

UPDATE

The following similar question also doesn't give a solution on how to change this default behaviour: Why does code first/EF use 'nvarchar(4000)' for strings in the raw SQL command?

From a db optimization point of view it makes sense, however when trying to reduce the amount data transferred to mobile clients in a slow network environment, it doesn't make sense to increase every string parameter to 4000 bytes.

来源:https://stackoverflow.com/questions/29041794/why-does-entity-framework-generate-large-parameters-how-can-they-be-reduced

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!