Linq with MySQL database in ASP.NET MVC 3 storing DateTime in a variable

I use the MySQL database to work in ASP.NET MVC 3, I have already configured all the requirements, and the connection is working fine. This code below works correctly and gives the correct result :

try { ViewBag.Model = (from n in _db.mainDatas where n.time_stamp == new DateTime(2010, 11, 3, 0, 0, 15) select n).Take(10).ToList(); }catch (Exception e) { ViewBag.Error = e; } 

But when I change this code to:

  DateTime test = new DateTime(2010,11,3,0,0,15); try { ViewBag.Model = (from n in _db.mainDatas where n.time_stamp == test select n).Take(10).ToList(); }catch (Exception e) { ViewBag.Error = e; } 

this error message is generated:

MySql.Data.MySqlClient.MySqlException: a fatal error occurred while executing a command. ---> MySql.Data.MySqlClient.MySqlException: Unable to serialize date / time value

I am using MySQL Connector / Net 6.3.6. Any solution to this problem?

+4
source share
2 answers

Well, after doing some searches and searches like crazy, I found a solution to my problem. Well, this is not a solution in fact, because it seems that MySQL Connector / Net 6.3.6 does not work very well with my project (possibly because of my server, database, project configuration, etc.), therefore I use Devart dotConnector instead of MySQL Connector / Net 6.3.6 and everything works like magic .: D

0
source

There seems to be a problem with the Linq to SQL provider for MySql that you used. In the first case, the part of the date "is" in the expression tree, which is generated from your linq query, where, as in the second case, the DateTime is declared as the side of the Linq query, and therefore, the generated expression tree will be different from the first. Now it depends on the expression tree parser in the Linq to SQL provider, how to handle both cases, and it seems that in this case the provider cannot correctly process the expression tree of the second case.

+2
source

All Articles