2014-12-02 59 views
2

我正在使用EF 6並嘗試使用全文搜索。我用IDbCommandInterceptor執行前修改查詢,所以結果查詢看起來是這樣的:使用實體框架進行全文搜索:由於未對列進行全文索引,因此無法在列上使用CONTAINS或FREETEXT謂詞

...AND (CONTAINS ([UserFullName], N''Римский '')) 

全文索引已經這樣

CREATE FULLTEXT INDEX 
ON [dbo].[Documents_CreditContracts](UserFullName, FullName, Number, StoreName, UserName) 
KEY INDEX PK_Documents_CreditContracts 

和這樣的查詢創建執行得好:

SELECT top 1000 * 
FROM [MKD_OneBillionDollars2].[dbo].[Documents_CreditContracts] 
WHERE CONTAINS(UserFullName, N'Римский ') 

但是當我執行EF查詢時,出現錯誤:

Cannot use a CONTAINS or FREETEXT predicate on column 'UserFullName' because it is not full-text indexed.

全EF查詢:

exec sp_executesql N'SELECT TOP (10) 
    [Project5].[C1] AS [C1], 
    [Project5].[Id] AS [Id], 
    [Project5].[DocumentTypeId] AS [DocumentTypeId], 
    [Project5].[Number] AS [Number], 
    [Project5].[FullName] AS [FullName], 
    [Project5].[Amount] AS [Amount], 
    [Project5].[SignDate] AS [SignDate], 
    [Project5].[UserFullName] AS [UserFullName], 
    [Project5].[TableNumber] AS [TableNumber], 
    [Project5].[BankId] AS [BankId], 
    [Project5].[UserBankCode] AS [UserBankCode], 
    [Project5].[StoreName] AS [StoreName], 
    [Project5].[StoreCode] AS [StoreCode], 
    [Project5].[StoreBankCode] AS [StoreBankCode], 
    [Project5].[UserName] AS [UserName] 
    FROM (SELECT [Project5].[Id] AS [Id], [Project5].[DocumentTypeId] AS [DocumentTypeId], [Project5].[Number] AS [Number], [Project5].[FullName] AS [FullName], [Project5].[Amount] AS [Amount], [Project5].[SignDate] AS [SignDate], [Project5].[UserFullName] AS [UserFullName], [Project5].[TableNumber] AS [TableNumber], [Project5].[BankId] AS [BankId], [Project5].[UserBankCode] AS [UserBankCode], [Project5].[StoreName] AS [StoreName], [Project5].[StoreCode] AS [StoreCode], [Project5].[StoreBankCode] AS [StoreBankCode], [Project5].[UserName] AS [UserName], [Project5].[C1] AS [C1], row_number() OVER (ORDER BY [Project5].[Id] ASC) AS [row_number] 
     FROM (SELECT 
      [Project1].[Id] AS [Id], 
      [Project1].[DocumentTypeId] AS [DocumentTypeId], 
      [Project1].[Number] AS [Number], 
      [Project1].[FullName] AS [FullName], 
      [Project1].[Amount] AS [Amount], 
      [Project1].[SignDate] AS [SignDate], 
      [Project1].[UserFullName] AS [UserFullName], 
      [Project1].[TableNumber] AS [TableNumber], 
      [Project1].[BankId] AS [BankId], 
      [Project1].[UserBankCode] AS [UserBankCode], 
      [Project1].[StoreName] AS [StoreName], 
      [Project1].[StoreCode] AS [StoreCode], 
      [Project1].[StoreBankCode] AS [StoreBankCode], 
      [Project1].[UserName] AS [UserName], 
      [Project1].[C1] AS [C1] 
      FROM (SELECT 
       [Extent1].[Id] AS [Id], 
       [Extent1].[DocumentTypeId] AS [DocumentTypeId], 
       [Extent2].[Number] AS [Number], 
       [Extent2].[FullName] AS [FullName], 
       [Extent2].[Amount] AS [Amount], 
       [Extent2].[SignDate] AS [SignDate], 
       [Extent2].[UserFullName] AS [UserFullName], 
       [Extent2].[TableNumber] AS [TableNumber], 
       [Extent2].[BankId] AS [BankId], 
       [Extent2].[UserBankCode] AS [UserBankCode], 
       [Extent2].[StoreName] AS [StoreName], 
       [Extent2].[StoreCode] AS [StoreCode], 
       [Extent2].[StoreBankCode] AS [StoreBankCode], 
       [Extent2].[UserName] AS [UserName], 
       ''0X0X'' AS [C1] 
       FROM [dbo].[Documents] AS [Extent1] 
       INNER JOIN [dbo].[Documents_CreditContracts] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id] 
      ) AS [Project1] 
      WHERE (EXISTS (SELECT 
       1 AS [C1] 
       FROM (SELECT [Extent3].[RegisterId] AS [RegisterId], [Extent3].[DocumentId] AS [DocumentId], [Join2].[Id1], [Join2].[C1] 
        FROM [dbo].[DocumentInPackage] AS [Extent3] 
        INNER JOIN (SELECT [Extent4].[Id] AS [Id1], [Project2].[C1] AS [C1] 
         FROM [dbo].[Registers] AS [Extent4] 
         LEFT OUTER JOIN (SELECT 
          [Extent5].[Id] AS [Id], 
          cast(1 as bit) AS [C1] 
          FROM [dbo].[Registers_CreditContractRegisters] AS [Extent5]) AS [Project2] ON [Extent4].[Id] = [Project2].[Id]) AS [Join2] ON [Extent3].[RegisterId] = [Join2].[Id1] 
        WHERE CASE WHEN (NOT (([Join2].[C1] = 1) AND ([Join2].[C1] IS NOT NULL))) THEN ''5X'' ELSE ''5X0X'' END LIKE ''5X0X%'') AS [Filter1] 
       LEFT OUTER JOIN (SELECT [Extent6].[Id] AS [Id2], [Project3].[OrpId] AS [OrpId], [Project3].[C1] AS [C1] 
        FROM [dbo].[Registers] AS [Extent6] 
        LEFT OUTER JOIN (SELECT 
         [Extent7].[OrpId] AS [OrpId], 
         [Extent7].[Id] AS [Id], 
         cast(1 as bit) AS [C1] 
         FROM [dbo].[Registers_CreditContractRegisters] AS [Extent7]) AS [Project3] ON [Extent6].[Id] = [Project3].[Id]) AS [Join4] ON [Filter1].[RegisterId] = [Join4].[Id2] 
       WHERE ([Project1].[Id] = [Filter1].[DocumentId]) AND ((CASE WHEN (CASE WHEN ([Join4].[Id2] IS NULL) THEN CAST(NULL AS varchar(1)) WHEN (NOT (([Join4].[C1] = 1) AND ([Join4].[C1] IS NOT NULL))) THEN ''5X'' ELSE ''5X0X'' END LIKE ''5X0X%'') THEN CASE WHEN ([Join4].[Id2] IS NULL) THEN CAST(NULL AS int) WHEN (NOT (([Join4].[C1] = 1) AND ([Join4].[C1] IS NOT NULL))) THEN CAST(NULL AS int) ELSE [Join4].[OrpId] END END) = @p__linq__0) 
      )) AND (CONTAINS ([UserFullName], N''Римский '')) 
     ) AS [Project5] 
    ) AS [Project5] 
    WHERE [Project5].[row_number] > 0 
    ORDER BY [Project5].[Id] ASC',N'@p__linq__0 int',@p__linq__0=1 

任何想法如何解決這個問題?

+1

我遇到了同樣的問題。我明白爲什麼它不起作用,但我不知道如何解決它。問題是Contains語句試圖在內部查詢的[Extent]部分的上下文中運行,該部分不包含全文索引。 – 2015-02-24 19:23:56

回答

0

我解決問題的方法是手動編寫sql查詢並執行它。並將結果映射到EF實體。很多的工作,yeap。

0

我知道答案是相對於詢問問題的日期,但我發現this answer有助於解決我的類似問題。

在此示例中,您使用攔截器(類型爲IDbCommandInterceptor)來調整在服務器上執行它之前由EF6 LINQ查詢生成的SQL。這樣您仍然可以利用LINQ功能,而無需構建自己的複雜SQL查詢生成器。

+0

yeap,我試過了。對我沒用 – 2015-09-22 15:44:47

相關問題