问题
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