如何在实体框架和 Mysql 中使用规范函数

how to use canonical functions in Entity Framework and Mysql(如何在实体框架和 Mysql 中使用规范函数)
本文介绍了如何在实体框架和 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 中使用规范函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)