You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using EF Core 2.1 and PostgreSQL. I have some entities that have a column in DateTimeOffset. Whenever I use these columns in a query the execution is happening on Client side. I would like to be able to do diffs on two DateTimeOffset on client side; I have also looked into DbFunctions but there seems to be no support for that either.
The following code is what I am trying to accomplish:
DateDiffSeconds is defined in SqlServerDbFunctionsExtensions, which means it's an SqlServer-specific method - there's no reason to expect it to work server-side on PostgreSQL.
The Npgsql provider does translate operations on DateTime, so you should be able to write .Where(x => x.ExpirationTime - DateTime.UtcNow > y). However, DateTimeOffset members aren't translated yet - will keep this issue open to track that.
In that meantime, consider using DateTime instead of DateTimeOffset (especially since PostgreSQL doesn't actually save the offset in the database).
Actually, thinking about this again, you should be able to subtract one DateTimeOffset from another, yielding a TimeSpan (just like you can subtract one DateTime from another). Give that a try.
Thank you for the quick response. I followed your recommendation and tried doing the following which did not work the query is still being executed on client side:
@SamHuo213 great, your working solution is actually the best one.
Regarding the one that isn't working, I'll try to dig deeper but it will probably take me some time to get around to it. Will keep this issue to track that.
Looking at this again, any query which has a DateTimeOffset member (such as UtcNow) will be evaluated client-side because the Npgsql provider currently doesn't translate any DateTimeOffset members. This issue tracks that, and once it's done you should be able to have expressions such as x.ExpirationTime - DateTimeOffset.UtcNow in your query and have that evaluated by PostgreSQL.
The workaround of having a local variable assigned to DateTimeOffset.UtcNow is ugly and not ideal, but it's probably the best temporary solution until translation is implemented.
Please note that DateTime's members are translated, so if you switch from DateTimeOffset to DateTime everything will work as expected. As a general rule DateTime is recommended over DateTimeOffset when using PostgreSQL, since the database doesn't actually save the offset component. Long story short, unless you're very clear on why you need DateTimeOffset (and these cases should be quite rare), it's really recommended you consider using DateTime instead. If you need any more info on this feel free to ask.
@roji , thanks for your reply. Yes I know that DateTime members are working. However, the decision to switch types is not up to me. I will try to get management to reconsider switching back to DateTime.
Hi guys. It's again me.
What about the case when I have to compare the Date part of the DateTime (DateTimeOffset).
Something like: .Where(x => x.Date.Date <= nowDateTimeOffset)
It looks like the issue is in the Date.Date request.
I have tried the DbFunctions: .Where(x => EF.Functions.DateDiffDay(x.Date, nowDateTimeOffset) <= 0)
But it doesn't work as well.
Does it mean that all DateTime value mutations before filtering are not supported?
Thank you! @roji
@roji Oh, thank you! So the problem not with the .Date but with the DateTimeOffset.Date. So DateTime.Date should work. Thank you! I didn't check the DateTime usecase yet.
DateDiffDay() returns the same error about the client side evaluation (as I remember). So nothing noted me that this will not work with NPG. And yes - I had to add the SqlServer Nuget package in order to get this function available. I thought that NPG support all functions from SQL + some own from PostgreExtensions
It really is discouraged to use timestamp with time zone unless you have a very good reason to do so - consider regular timestamp with DateTime on the. NET side. timestamp with time zone is a quirky data type that is better avoided in most scenarios (this is also why translation of members on DateTimeOffset haven't been implemented yet).
SQL Server is a different database product with it's own provider, anything coming from that package will only work with that provider, not with Npgsql. Many generic components and translations do exist, but they're in the core of EF Core and in not in the SQL Server package.
Apparently DateDiffDay() contains a C# implementation, which means you can do client evaluation. This is why it worked (but triggered client evaluation).
Activity
roji commentedon Jun 18, 2018
DateDiffSecondsis defined in SqlServerDbFunctionsExtensions, which means it's an SqlServer-specific method - there's no reason to expect it to work server-side on PostgreSQL.The Npgsql provider does translate operations on
DateTime, so you should be able to write.Where(x => x.ExpirationTime - DateTime.UtcNow > y). However,DateTimeOffsetmembers aren't translated yet - will keep this issue open to track that.In that meantime, consider using
DateTimeinstead ofDateTimeOffset(especially since PostgreSQL doesn't actually save the offset in the database).[-]DateTimeOffset diff is executed on client side[/-][+]Translate DateTimeOffset members[/+]roji commentedon Jun 18, 2018
Actually, thinking about this again, you should be able to subtract one DateTimeOffset from another, yielding a TimeSpan (just like you can subtract one DateTime from another). Give that a try.
SamHuo213 commentedon Jun 18, 2018
Thank you for the quick response. I followed your recommendation and tried doing the following which did not work the query is still being executed on client side:
However, I did do this and it seems to be working on server side now:
roji commentedon Jun 20, 2018
@SamHuo213 great, your working solution is actually the best one.
Regarding the one that isn't working, I'll try to dig deeper but it will probably take me some time to get around to it. Will keep this issue to track that.
sguryev commentedon Jul 4, 2018
@SamHuo213 thank you for your post. So the trick is to save the static property value to the local variable?
roji commentedon Jul 4, 2018
Looking at this again, any query which has a DateTimeOffset member (such as
UtcNow) will be evaluated client-side because the Npgsql provider currently doesn't translate any DateTimeOffset members. This issue tracks that, and once it's done you should be able to have expressions such asx.ExpirationTime - DateTimeOffset.UtcNowin your query and have that evaluated by PostgreSQL.The workaround of having a local variable assigned to
DateTimeOffset.UtcNowis ugly and not ideal, but it's probably the best temporary solution until translation is implemented.Please note that DateTime's members are translated, so if you switch from
DateTimeOffsettoDateTimeeverything will work as expected. As a general ruleDateTimeis recommended overDateTimeOffsetwhen using PostgreSQL, since the database doesn't actually save the offset component. Long story short, unless you're very clear on why you needDateTimeOffset(and these cases should be quite rare), it's really recommended you consider usingDateTimeinstead. If you need any more info on this feel free to ask.SamHuo213 commentedon Jul 4, 2018
@sguryev , that is correct. Avoiding DateTimeOffset members is my work around. DateTime members such as Now & UtcNow are working. Looks like it is being explicitly translated here: https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/blob/dev/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlDateTimeMemberTranslator.cs.
@roji , thanks for your reply. Yes I know that DateTime members are working. However, the decision to switch types is not up to me. I will try to get management to reconsider switching back to DateTime.
sguryev commentedon Jul 13, 2018
Hi guys. It's again me.
What about the case when I have to compare the Date part of the
DateTime(DateTimeOffset).Something like:
.Where(x => x.Date.Date <= nowDateTimeOffset)It looks like the issue is in the
Date.Daterequest.I have tried the
DbFunctions:.Where(x => EF.Functions.DateDiffDay(x.Date, nowDateTimeOffset) <= 0)But it doesn't work as well.
Does it mean that all
DateTimevalue mutations before filtering are not supported?Thank you!
@roji
roji commentedon Jul 13, 2018
@sguryev,
DateTime.Dateis properly translated to SQL, producing the following:However, as written above, no members of DateTimeOffset are currently translated - this is what this issue is about.
Finally,
DateDiffDay()is an SQL Server-specific extension and doesn't work when using Npgsql.sguryev commentedon Jul 13, 2018
@roji Oh, thank you! So the problem not with the
.Datebut with theDateTimeOffset.Date. SoDateTime.Dateshould work. Thank you! I didn't check theDateTimeusecase yet.DateDiffDay()returns the same error about the client side evaluation (as I remember). So nothing noted me that this will not work with NPG. And yes - I had to add theSqlServerNuget package in order to get this function available. I thought that NPG support all functions from SQL + some own from PostgreExtensionsroji commentedon Jul 13, 2018
It really is discouraged to use
timestamp with time zoneunless you have a very good reason to do so - consider regulartimestampwithDateTimeon the. NET side.timestamp with time zoneis a quirky data type that is better avoided in most scenarios (this is also why translation of members onDateTimeOffsethaven't been implemented yet).SQL Server is a different database product with it's own provider, anything coming from that package will only work with that provider, not with Npgsql. Many generic components and translations do exist, but they're in the core of EF Core and in not in the SQL Server package.
Apparently
DateDiffDay()contains a C# implementation, which means you can do client evaluation. This is why it worked (but triggered client evaluation).33 remaining items
Redo timestamp handling
Redo timestamp handling
Redo timestamp handling
Redo timestamp handling
Redo timestamp handling
Redo timestamp handling
Redo timestamp handling
Redo timestamp handling
Redo timestamp handling
Redo timestamp handling
Redo timestamp handling
Redo timestamp handling (#1992)