8

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 https://msdn.microsoft.com/en-us/library/bb738563.aspx

I am using

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

2
  • have you added System.Data.Entity namespace to usings? May 14, 2015 at 11:06
  • @AdilMammadov yes it is already there. May 14, 2015 at 11:09

2 Answers 2

2

Actually i misunderstood the error it is not

FUNCTION projectName.AddMinutes does not exist

but

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

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

0

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

DateTime dateNow = new DateTime.Now;
dateNow.AddMinutes(someminutes);

DbFunctions.AddMinutes(expression, number) this function can only appear within a LINQ to Entities query (https://msdn.microsoft.com/en-us/library/dn220081(v=vs.113).aspx):

Context.Train.Where(x => EntityFunctions.AddMinutes(x.Start, 10) > DateTime.Now)
4
  • Are you sure that your driver to mysql supports canonical functions?
    – PatBusz
    May 14, 2015 at 13:12
  • how can i check that driver supports canonical functions or not? May 14, 2015 at 15:35
  • Yesterday I checked yours linq query on MSSQL Database and it is work. Today, I am going to install MySql Database on my Windows and check yours linq query. I'll write you what kind of connector I use on condition that it works.
    – PatBusz
    May 15, 2015 at 6:08
  • FUNCTION projectName.AddMinutes does not exist abutally it is FUNCTION databaseName.AddMinutes does not exist. I did a hack to solve this issue. i created AddMinutes Function which internally call DATE_ADD() May 15, 2015 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.