4
我有下面的代碼首先實體:實體框架中包含查詢在VARCHAR列
public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
}
我已成立的配置作如下表:
dbo.Departments
DepartmentID INT PRIMARY KEY
DepartmentName VARCHAR(75)
我添加一個索引DepartmentName的手動並運行以下查詢:
var depts = new string[] { "Engineering", "Manufacturing" };
var departments = from d in db.Departments
where depts.Contains(d.DepartmentName)
select d;
當我運行跟蹤時,它生成以下SQL :
SELECT
[Extent1].[DepartmentID] AS [DepartmentID],
[Extent1].[DepartmentName] AS [DepartmentName]
FROM [dbo].[Departments] AS [Extent1]
WHERE [Extent1].[DepartmentName] IN (N'Engineering',N'Manufacturing')
由於我的列是一個varchar和查詢轉換文本NVARCHAR,優化是選擇做一個聚集索引掃描,而不是索引的尋求我的DepartmentName的指數。
有什麼辦法不是改變我的數據庫使用nvarchar的解決這個問題其他?這是一個錯誤還是這是正常的?
我不認爲你可以改變傳遞到一些配置的查詢參數的類型,但是你可以下載EF的源代碼,並修改它自己只使用'VARCHAR'。 –