how to use canonical functions in Entity Framework and Mysql

c# entity-framework entity-framework-6 mysql


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

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

FUNCTION projectName.AddMinutes does not exist

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

I am using

  1. MySql.Data.Entities
  2. EntityFramework 6.0.0
  3. MySql.Data 6.8.4
  4. MySql.Web 6.8.4
  5. MySql (Database) 5.6.17

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

5/15/2015 8:56:15 AM

Accepted Answer

Actually i misunderstood the error it is not

FUNCTION projectName.AddMinutes does not exist


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.

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
    RETURN DATE_ADD(actualDateTime, INTERVAL minutesToAdd MINUTE);

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

5/15/2015 8:50:52 AM

Popular Answer

AddMinutes method is part of DateTime class, so you can use to instance of DateTime class i.e.

DateTime dateNow = new DateTime.Now;

DbFunctions.AddMinutes(expression, number) this function can only appear within a LINQ to Entities query (

Context.Train.Where(x => EntityFunctions.AddMinutes(x.Start, 10) > DateTime.Now)

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow