-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Query: TimeSpan with DateTime arithmetic operations are not supported #6025
Comments
Note for triage: It seems this was also a problem with the old stack, and from a little searching it doesn't seem like there is an obviously better type mapping. |
Just found this on the backlog: #770 |
@divega we want to discuss this with you when you are back |
EFC 2.0 RC
Well, year has passed, still can't work with |
Clearing up milestone do that we can discuss in triage. |
Notes for triage: Looks like a couple of different things are happening here. In original bug, TimeSpan was being converted to Time, but the value overflowed. This is somewhat expected since the mapping for TimeSpan is to Time, but Time can easily overflow. This is discussed in #242 for mapped properties. The new exception is different--it indicates that query is attempting to create a DateTime parameter but using a TimeSpan object. SqlClient cannot handle this and so throws. I suspect this is happening due to type inference in the query pipeline, but I haven't verified this. So, fundamentally, this issue is not about the general mapping of TimeSpan, which is covered by #242, but instead is about what query should do when translating this either in terms of creating parameters of the "correct" type (which can overflow) or doing some more complex translation which avoids this. @AsValeO As a workaround, you can force client evaluation of the query: q = q.ToList().Where(x => x.Added + TimeSpan.FromDays(3) >= DateTime.Now); Before RC1 client evaluation was likely happening all the time, which is why it appeared to be "working". |
Confirming @ajcvickers findings. The second exception is from a query bug, but even if fixed it would result in the first exception anyway. |
Another workaround is to calculate the deadline on the client: var deadline = DateTime.Now + TimeSpan.FromDays(3);
q = q.Where(x => x.Added >= deadline); But both of these workarounds suffer from |
Since The best work around to achieve this in SqlServer would be to use var bs = await db.Building.AsNoTracking()
.Where(
x => DateTime.Now > x.AddDate.AddDays(30)).ToListAsync(); If you are using different It will also avoid issue of overflow. |
There are multiple issues around this area.
Closing the issue as the work-around translate to server & also avoid overflow issue. We can address client eval or future translation if there is enough customer feedback. |
@divega - Can you put appropriate labels for this issue? I am not sure what is correct one. |
@smitpatel I found myself copying the majority of the contents of the issue. I ended up repurposing it instead. Feel free to improve the title or the first comment. |
I throw my obvious suggestion here too: Map it like this: It would not cover microseconds but I think most often TimeSpans need values greater than 24 hours, than precision of microseconds. And when TimeSpan is just a integer of seconds, the queries are easier to compose I think. |
@smitpatel to comment. |
Filed #18939 for subset of these we can actually translate. Closing this issue as won't fix. |
Edit by @divega:
Fixing this issue is not necessarily about fixing the overflow or type inference problems we hit immediately with the different expressions in this bug but about finding translations of those expressions that actually work. Likely when we find expressions like this we could transform at least some of them into a combination of
DateTime.Add
andDateTime.Substract
, client evaluation, etc., that don't even require creating parameters of typeTimeSpan
.Original customer report
The issue
After updating from RC1 this issue with
TimeSpan
andSqlDbType.Time
appeared.Building.AddDate
type isdatetime
in Azure SQL DB,DateTime
in generated entity class.Further technical details
DB: Azure SQL DB
EF Core version: 1.0.0
Operating system: Windows Server 2012
Visual Studio version: 2015U3
The text was updated successfully, but these errors were encountered: