Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Data.SqlClient.SqlException (0x80131904): The datepart hour is not supported by date function dateadd for data type date. #9566

Closed
doublnt opened this issue Aug 25, 2017 · 8 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@doublnt
Copy link

doublnt commented Aug 25, 2017

Using .NET-Core 2.0 and EF-Core 2.0
When I run this code in ubuntu16.04 in Linux , then log show the error, but it is fine when I use in windows

public async Task<int> GetTodayQuestionCount()
{
    return await Entities
               .Where(q => q.IsActive && q.DateAdded >= DateTime.Now.Date && q.DateAdded < DateTime.Now.Date.AddHours(8))
               .Select(q => q.QID).CountAsync();
}
@smitpatel
Copy link
Member

Can you post debug log output for query in both machines? Ideally, EF Core should generate same query. Also are you connecting to same server from both machines?

@doublnt doublnt closed this as completed Aug 29, 2017
@ErikEJ
Copy link
Contributor

ErikEJ commented Aug 29, 2017

@doublnt Pls share why this was closed!?

@doublnt
Copy link
Author

doublnt commented Aug 29, 2017

@ErikEJ Sorry, I forgot.Following: I try to separate the query conditions like that

public async Task<int> GetTodayQuestionCount()
{
    var startTime = DateTime.Now.Date;
    var endTime = startTime.addHours(8);
    return await Entities
               .Where(q => q.IsActive && q.DateAdded >= startTime && q.DateAdded < endTime)
               .Select(q => q.QID).CountAsync();
}

But I didn't test in my production Environment. I will show the result after it is work in my Production Environment.

@doublnt
Copy link
Author

doublnt commented Sep 15, 2017

@ErikEJ It's work when I use in my production environment

@uladz-zubrycki
Copy link

uladz-zubrycki commented Apr 14, 2021

I encounter the same error for EF 3.1.12 with date_value.Date.AddHours(hours_value)

It fails since date_value.Date is converted to sql as CONVERT(date, date_value) and then .AddHours which is DATEADD(hour, date_value, hours_value) obviously fails as resulting expression is DATEADD(hour, CONVERT(date, date_value), hours_value) and you can't add hours to value of type date.

Using this as a workaround, but not sure whether it won't fail with overflow once

date_value
    .AddMilliseconds(-EF.Functions.DateDiffMillisecond(TimeSpan.Zero, date_value.TimeOfDay))
    .AddHours(hours_value)

@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. customer-reported labels Nov 16, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
@uladz-zubrycki
Copy link

@ajcvickers Could you please elaborate on why it's not planned? Developers are forced to use quite a hacky and dirty approach to deal with this issue, which isn't that obvious without additional investigation.

@ajcvickers
Copy link
Member

@uladz-zubrycki It was closed by the original poster. If you think there is a bug here, then please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

@uladz-zubrycki
Copy link

@ajcvickers Indeed. This change confused me
выява

As to the reproduction, original snippet is enough. And here described a reason of such behavior #9566 (comment)

It's applicable for EF 3.1.12, haven't tried that with the latest EF version since project, where I faced this problem, is locked to EF 3.
It's the best I could do here, the rest is up to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

5 participants