【问题标题】:CurrentUtcDateTime does not exist - Entity Framework and MySqlCurrentUtcDateTime 不存在 - 实体框架和 MySql
【发布时间】:2011-08-10 19:41:05
【问题描述】:

我在使用 Entity Framework 4.1 和 MySql Connector/Net 6.4.3 中的规范函数时遇到问题。 根据微软规范,所有数据库提供者从 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;
    }

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

这是错误输出:

*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*

我错过了什么吗?请指教。谢谢。

【问题讨论】:

    标签: mysql entity-framework


    【解决方案1】:

    我遇到了同样的问题,花了将近两天的时间试图弄清楚。它似乎是 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);
    

    【讨论】:

    • 我认为自己很幸运能够如此迅速地找到您的答案。疯了,这个错误在 4 年后仍未修复。
    • 刚刚也遇到了这个;荒谬的。我想没有人在听错误报告。只需将其添加到实体设计器的“IsPrimaryKey”错误中,仍然存在一段时间。
    【解决方案2】:

    根据 Bohemian 的建议,我用“绕过”功能解决了这个问题。

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

    【讨论】:

    • 我不认为这是复制安全的。如果我有两个数据库并在一个上执行此功能,然后将其复制到另一个,它将产生单独的结果。我认为这里不应该使用“DETERMINISTIC”。
    • 不是一个好的解决方案,麻烦不断将此功能添加到新环境服务器。
    【解决方案3】:

    【讨论】:

    • 感谢您的回复。 SQL是EF自动生成的,所以不能改函数,还是有开关可以做(这是我第一个EF项目,还在学习中)。
    猜你喜欢
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多