2011-08-10 24 views
2

我在Entity Framework 4.1和MySql Connector/Net 6.4.3中遇到了規範函數的問題。 根據微軟的cannonical函數,所有數據庫提供者都可以從LINQ生成的SQL中理解和翻譯成本地SQL方言; http://msdn.microsoft.com/en-us/library/bb738626.aspx但是,我在此處列出的CurrentUtcDateTime()上的代碼扼流圈; http://msdn.microsoft.com/en-us/library/bb738563.aspxCurrentUtcDateTime不存在 - Entity Framework和MySql

這裏是LINQ查詢(從NopCommerce),其產生的進攻SQL:

public List<Poll> GetPolls(int languageId, int pollCount, bool loadShownOnHomePageOnly) 
    { 
     bool showHidden = NopContext.Current.IsAdmin; 


     var query = (IQueryable<Poll>)_context.Polls; 
     if (!showHidden) 
     { 
      query = query.Where(p => p.Published); 
      query = query.Where(p => !p.StartDate.HasValue || p.StartDate <= DateTime.UtcNow); 
      query = query.Where(p => !p.EndDate.HasValue || p.EndDate >= DateTime.UtcNow); 
     } 
     if (loadShownOnHomePageOnly) 
     { 
      query = query.Where(p => p.ShowOnHomePage); 
     } 
     if (languageId > 0) 
     { 
      query = query.Where(p => p.LanguageId == languageId); 
     } 

     query = query.OrderBy(p => p.DisplayOrder); 
     if (pollCount > 0) 
     { 
      query = query.Take(pollCount); 
     } 

     var polls = query.ToList(); 

     return polls; 
    } 

query.ToList()生成以下SQL:

SELECT`Project1`.`PollID`, `Project1`.`LanguageID`, `Project1`.`Name`, 
`Project1`.`Published`, `Project1`.`ShowOnHomePage`, `Project1`.`DisplayOrder`, 
`Project1`.`SystemKeyword`, `Project1`.`StartDate`, `Project1`.`EndDate` 
FROM (SELECT`Extent1`.`PollID`, `Extent1`.`LanguageID`, `Extent1`.`Name`, 
`Extent1`.`SystemKeyword`, `Extent1`.`Published`, `Extent1`.`ShowOnHomePage`, 
`Extent1`.`DisplayOrder`, `Extent1`.`StartDate`, `Extent1`.`EndDate` 
FROM `Nop_Poll` AS `Extent1` WHERE ((((`Extent1`.`Published` = 1) AND 
((`Extent1`.`StartDate` IS NULL) OR (`Extent1`.`StartDate` <= (CurrentUtcDateTime())))) 
AND ((`Extent1`.`EndDate` IS NULL) OR (`Extent1`.`EndDate` >= (CurrentUtcDateTime())))) 
AND (`Extent1`.`ShowOnHomePage` = 1)) AND (`Extent1`.`LanguageID` = @p__linq__0)) 
AS `Project1` ORDER BY `Project1`.`DisplayOrder` ASC LIMIT 2147483647 

這是錯誤outputed:

*FUNCTION myDatabase.CurrentUtcDateTime does not exist 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: MySql.Data.MySqlClient.MySqlException: FUNCTION myDatabase.CurrentUtcDateTime does not exist* 

我錯過了什麼嗎?請指教。謝謝。

回答

-1
+1

感謝您的回覆。 SQL由EF自動生成,因此我無法更改該功能,或者是否有切換操作(這是我的第一個EF項目,所以我仍在學習)。 – user888112

8

我遇到了這個確切的同樣的問題,失去了近兩天試圖弄明白。它似乎是MySql的EntityFramework映射中的一個錯誤。

解決的辦法是將DateTime.UtcNow計算移到範圍的lambda之外並插入實際值。

var utcNow = DateTime.UtcNow; 
query = query.Where(p => p.Published); 
query = query.Where(p => !p.StartDate.HasValue || p.StartDate <= utcNow); 
query = query.Where(p => !p.EndDate.HasValue || p.EndDate >= utcNow); 
+2

我覺得我很幸運,很快就偶然發現了你的答案。瘋狂,這個錯誤仍然沒有被固定4年後。 – Kazu

+0

剛剛遇到了這個;荒謬。我猜沒有人在聽錯誤報告。只需將此添加到實體設計器的「IsPrimaryKey」錯誤中,仍然存在一段時間。 –

0

基於波希米亞的建議,我定了「搭橋」作用這一問題。

CREATE FUNCTION `your_schema`.`CurrentUtcDateTime`() 
RETURNS TIMESTAMP DETERMINISTIC 
RETURN UTC_TIMESTAMP(); 
+1

我不認爲這是複製安全。如果我有兩個數據庫並在一個上執行此功能,然後它複製到另一個,它會產生單獨的結果。我認爲'DETERMINISTIC'不應該在這裏使用。 –