use*_*112 3 mysql entity-framework
我在Entity Framework 4.1和MySql Connector/Net 6.4.3中遇到了规范函数的问题.根据Microsoft的说法,所有数据库提供者都可以从LINQ生成的SQL中理解并将其转换为本地SQL方言; http://msdn.microsoft.com/en-us/library/bb738626.aspx但是,我的代码在CurrentUtcDateTime()上窒息,这里列出了; http://msdn.microsoft.com/en-us/library/bb738563.aspx
以下是生成攻击性SQL的LINQ查询(来自NopCommerce):
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;
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
这是错误输出:
*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*
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?请指教.谢谢.
小智 11
我遇到了这个完全相同的问题,并且失去了将近两天试图找出它.它似乎是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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1866 次 |
| 最近记录: |