简体   繁体   中英

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

I am using MySQL database to work in ASP.NET MVC 3, i've already set up all requirements and connection is working fine. This code below is working properly and produce right 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 into:

        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: Fatal error encountered during command execution. ---> MySql.Data.MySqlClient.MySqlException: Unable to serialize date/time value

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

It seems to be a problem with the Linq to SQL provider for MySql that you have been using. In first case the date part is "in" the Expression tree that is generate from your linq query where as in the second case the DateTime is declared out side of the Linq query and hence the generated expression tree will be different from the first case. Now it depends on the parser of expression tree in the Linq to SQL provider how to handle both the cases and it seems in this case the provider is not able to properly handle the second case expression tree.

Ok, after doing some searches and googling like crazy, finally I found the solution for my problem. Well, it's not a solution actually, because it looks like MySQL Connector/Net 6.3.6 is not doing well with my project (maybe because of my server, database, project configurations, etc) so I use Devart dotConnector instead of MySQL Connector/Net 6.3.6 and everything works like magic. :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM