我在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*
我錯過了什麼嗎?請指教。謝謝。
感謝您的回覆。 SQL由EF自動生成,因此我無法更改該功能,或者是否有切換操作(這是我的第一個EF項目,所以我仍在學習)。 – user888112