如何在实体框架和 Mysql 中使用规范函数 [英] how to use canonical functions in Entity Framework and Mysql

查看:12
本文介绍了如何在实体框架和 Mysql 中使用规范函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 MySql 数据库在 EntityFramework 中将时间跨度添加到 DateTime.

I want to add Timespan to DateTime in EntityFramework with MySql Database.

我尝试过使用 DbFunctions.AddMinutes(someminutes)EntityFunctions.AddMinutes(someminutes) 但是当我执行时我得到类似

i have tried using DbFunctions.AddMinutes(someminutes) and EntityFunctions.AddMinutes(someminutes) but when i execute i get exception something like

FUNCTION projectName.AddMinutes 不存在

FUNCTION projectName.AddMinutes does not exist

我已经用谷歌搜索但找不到如何执行规范函数.虽然有一个函数列表,但我还是不知道它们属于哪个类https://msdn.microsoft.com/en-us/library/bb738563.aspx

i have googled but cannot find how to execute canonical function. though there is a list of function but again i don't know which class they belong https://msdn.microsoft.com/en-us/library/bb738563.aspx

我正在使用

  1. MySql.Data.Entities 6.8.3.0
  2. EntityFramework 6.0.0
  3. MySql.Data 6.8.4
  4. MySql.Web 6.8.4
  5. MySql(数据库)5.6.17

我的 Linq 查询如下

My Linq Query is as below

IQueryable<OrderViewModel> orders = _dbContext.Orders
                         .OrderByDescending(x => x.ID)
                         .Select(x => new OrderViewModel 
                                        { ID = x.ID,
                                          AddedOn = DbFunctions.AddMinutes(x.AddedOn, diffMinutes).Value, 
                                          Customer = (x.IsGuestCheckOut == true ? x.CustomerEmail : x.Customer.FirstName + " " + x.Customer.LastName), 
                                          Phone = x.Phone, 
                                          TotalAmount = x.TotalAmount, 
                                          OrderStatus = x.OrderStatus });

在某些应用条件和分页的地方

down the road some where condition and pagination is applied

推荐答案

其实是我误会了错误不是

Actually i misunderstood the error it is not

FUNCTION projectName.AddMinutes 不存在

FUNCTION projectName.AddMinutes does not exist

但是

FUNCTION databaseName.AddMinutes 不存在

FUNCTION databaseName.AddMinutes does not exist

我不知道问题是什么.我有不兼容的驱动程序/连接器吗,我不知道.

I don't know what the issue is. Do i have any non-compatible driver/connector , I don't know.

为了解决这个问题,我刚刚创建了名为 AddMinutes 的函数,它在内部调用 DATE_ADD() 函数.函数定义如下

To solve this problem i just created function with name AddMinutes, it calls DATE_ADD() function internally. function definition is as below

CREATE FUNCTION `AddMinutes`(actualDateTime datetime, minutesToAdd int)
RETURNS datetime
BEGIN
    RETURN DATE_ADD(actualDateTime, INTERVAL minutesToAdd MINUTE);
END

我知道这不是正确的解决方案,而是 HACK

I understand that this is not proper solution, but a HACK

这篇关于如何在实体框架和 Mysql 中使用规范函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆