Pomelo.EntityFrameworkCore.MySql
Pomelo.EntityFrameworkCore.MySql copied to clipboard
AddDays does not properly get translated as a MySql Statement
Simple AddDays example
using (var context = serviceProvider.GetService<MyContext>())
{
double daysDiff = 5;
var posts = await context.BlogPosts
.Where(p => p.PublicationDate.AddDays(daysDiff) < DateTimeOffset.Now)
.ToListAsync();
}
Generated SQL:
SELECT `p`.`Id`, `p`.`CategoryId`, `p`.`Content`, `p`.`PublicationDate`, `p`.`Title`
FROM `BlogPosts` AS `p`
WHERE DATE_ADD(`p`.`PublicationDate`, INTERVAL __daysDiff_0 day) < @__Now_1
(note that __daysDiff_0 is not a parameter)
Exception: Unknown column '__daysDiff_0' in where clause
Implicit variable
using (var context = serviceProvider.GetService<MyContext>())
{
var daysDiff = 5;
var posts = await context.BlogPosts
.Where(p => p.PublicationDate.AddDays(daysDiff) < DateTimeOffset.Now)
.ToListAsync();
}
Generated SQL:
SELECT `p`.`Id`, `p`.`CategoryId`, `p`.`Content`, `p`.`PublicationDate`, `p`.`Title`
FROM `BlogPosts` AS `p`
WHERE DATE_ADD(`p`.`PublicationDate`, INTERVAL Convert(__daysDiff_0, Double) day) < @__Now_1
Exception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Double) day) < timestamp('2018-05-16 02:10:21.444264')'
Repository with example reproductions: https://github.com/AlphaGit/mysql-ef-pomelo-add-days-issue
Further technical details
MySQL version: 8.0 Operating system: Windows 10 Pomelo.EntityFrameworkCore.MySql version: 2.0.1