I have this sample query:
context.BarcodeTipiDoc.AsQueryable().Where(d => d.Barcode.CompareTo(minBarcode) > 0);
That query runs very slow because Entity Framework creates SqlParameter for "minBarcode" as nvarchar
instead of varchar
.
I tried to set column mapping:
[Column("Barcode", TypeName = "varchar(21)")]
public string Barcode { get; set; }
but nothing changed.
There is a way to tell to Entity Framework the right type of the sqlparameter?
This query is almost instantaneous:
DECLARE @__minBarcode_0 AS Varchar(21)
SET @__minBarcode_0 = 'aa'
SELECT TOP(100) [d].[Barcode], [d].[contenttype], [d].[idvolume], [d].[path_documento], [d].[Progressivo], [d].[Stato]
FROM BarcodeTipiDoc AS [d]
WHERE [d].[Barcode] > @__minBarcode_0
Same query generated by Entity Framework, takes several minutes because of nvarchar:
DECLARE @__minBarcode_0 AS nvarchar(21)
SET @__minBarcode_0 = 'aa'
SELECT TOP(100) [d].[Barcode], [d].[contenttype], [d].[idvolume], [d].[path_documento], [d].[Progressivo], [d].[Stato]
FROM BarcodeTipiDoc AS [d]
WHERE [d].[Barcode] > @__minBarcode_0
table schema:
Barcode varchar(21) Unchecked
tipodoc char(4) Unchecked
codutenteinserimento uniqueidentifier Checked
dataacquisizione datetime Checked
firmato bit Checked
tipodocdescrizione varchar(50) Checked
Stato int Unchecked
originedoc tinyint Checked Unchecked
I am not allowed to change anything of the database, I just need to change generated sql code from LINQ by entity framework.
If there are no ways, I'll be forced to write and the execute the select as pure string.
The version is entity framework 2.2, but I can upgrade.