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
{{ message }}
Repository navigation
Regarding the "It is no longer possible to write DateTimeOffset with offsets other than 0" breaking change, can we opt out of THIS behavior alone? #2108
Just started upgrading from .NET 5 to .NET 6, including NPGSQL EF Core 6. All went smoothly for the most part till I encountered this breaking change:
It is no longer possible to write DateTimeOffset with offsets other than 0 (UTC), since these cannot be represented in PostgreSQL. These were previously implicitly converted to UTC before sending.
Reading through the changelog, I can understand how 6.0 wants to "rationalizes" the whole thing and try to make it "UTC everywhere".
The thing is, many other .NET developers, me included, have always learned to use DateTimeOffset and avoid DateTime in .NET. In situations where the exact time zone and offset context is also required, we'd store the time zone info separately. This is not as perfect as using the superior NodaTime types, but it works fine if we want to stick to the .NET native types for now.
This means at first glance, the datetime related breaking changes in 6.0 shouldn't have affected us, because our entities were already using DateTimeOffset and they were already mapped to timestamptz in PostgreSQL.
That is until the aforementioned breaking change broke the database writing operations everywhere.
What is wrong with the automatic "implicitly conversion" behavior in the previous versions?
What difference does it make for the developer to convert this to 0 before saving, vs the EF Core provider doing it?
We do not actually care what particular UTC offset is stored in the DateTimeOffset object if all we want is to make sure it's an absolute time – and if we do care about the offset, we'd store a time zone string in the PostgreSQL database along with the timestamptz so that we can calculate the offset if needed.
This means the pervious implicit conversion was fine, and it was actually handy.
This is because date and time related values are often obtained directly or parsed from various 3rd party APIs/systems/SDKs, such as MS Graph/Google API. We sometimes have little or no control, nor do we really want to care, which UTC offsets were in these values, as long as we can make sure they're an absolute time and we have a DateTimeOffset object to work with.
To accommodate this breaking change, we're forced to either
A. Set the global AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true) or
B. Try to dig into every single DateTimeOffset assignment in our codes to make sure they had either UTC 0 or converted to 0 prior to being saved in EF Core.
We don't want to go full "legacy", especially if we may be able to take advantage of other new features and improvements in 6.0 and future version. But option B can obviously become rather tedious and difficult.
Given this context, can the NPGSQL EF Core team consider exposing a separate option to opt back into the automatic conversion behavior, or revert this change?
First, I'd recommend giving this blog post which contains a lot of the reasoning behind all these changes.
What is wrong with the automatic "implicitly conversion" behavior in the previous versions?
Quite a few things... First, many users writing DateTimeOffset to PostgreSQL expected the offset to be saved. That's a very reasonable expectation, especially considering that SQL Server (which many people come from) does have a datetimeoffset type; this caused a lot of confusion. The exception lets people know up-front that it's not possible to fully and faithfully represent the type in the database.
Related to the above, the conversions made things even more confusing, since DateTimeOffsets wouldn't be roundtripped: you write a non-UTC DateTimeOffset, but when you read back the same thing, you get a UTC DateTimeOffset. It's also not possible to convert symmetrically: when writing, we can convert non-UTC to UTC, but we can't convert back when reading (previous versions actually converted to the local timezone of the machine, which is an even worse idea...).
In short, I believe a database driver really should faithfully read and write your data to the database, and not perform implicit conversions on it; any under-the-hood magic here is harmful and makes things harder to understand.
What difference does it make for the developer to convert this to 0 before saving, vs the EF Core provider doing it?
It's all the difference in the world - when the developer makes a conscience decision to convert, they're aware of it and are explicitly opting into it.
B. Try to dig into every single DateTimeOffset assignment in our codes to make sure they had either UTC 0 or converted to 0 prior to being saved in EF Core.
There's really no need for that - if you want to systematically convert all DateTimeOffsets prior to sending, simply set up an EF Core value converter once, and you're done. Although I'd argue that if you're really doing UTC everywhere, then you shouldn't even have a non-UTC DateTimeOffset coming anywhere near EF Core - you should be converting to UTC much earlier, at the edges when reading the value.
Re adding another behavior switch for this... Every switch increases complexity (code maintenance, testing, user bug report analysis) so I generally don't like to do that unless there's ample user need. We can see whether other users run into this, but given that there's an easy workaround (value converters) I don't think it's warranted at this point.
@roji Thanks for the thorough explanation on the history behind these changes. The root of all the mess was pretty much the fact that PostgreSQL simply doesn't have a DateTimeOffset type, and yet they somehow chose to name their timestamptz type in a misleading way to make it sound like they do. 👿
I forgot about using value converters on a built-in type (just didn't have to do it before). Also my first thought after your post was "but don't I still have to add the converter to every DateTimeOffset property in every entity class...", but then found that EF Core 6.0 thankfully did introduce HaveConversion, so I will go with this approach for now.
But to circle back to the original point about this breaking change...
I can totally see how the reading behavior changes were totally justified. As you said, things like using local machine timezone etc were obviously bad ideas. And those changes actually have effects.
But the writing change cannot change the fact that no matter what you do at EF Core/.NET, the timestamptz column shall remain what it is – storing an absolute point in time in UTC format. So you can ask us to manage or manipulate our DateTimeOffset in our codes however you think is right, or set up value converters etc, but at the end of the day, as long as we continue using the timestamptz column (and we have no other choice), nothing really changes, right?
So that exception, which caused the breaking change, is effectively only informational. There actually isn't anything we can do to "fix" or change this specific PostgreSQL type. That we can and should store time zone separately if needed is true regardless if you throw that exception or not.
All the codes we need to add/change (like in my case, the value converter), only served one purpose: to satisfy this breaking change itself so that we don't get these runtime exceptions.
So isn't it more suitable to make this some warning that can be acknowledged and suppressed, rather than a runtime exception?
The root of all the mess was pretty much the fact that PostgreSQL simply doesn't have a DateTimeOffset type, and yet they somehow chose to name their timestamptz type in a misleading way to make it sound like they do.
It indeed partially has to do with this... FWIW apparently the timestamptz type (and name) is specified by the SQL standard, rather than a PG invention. Also, I'm personally happy PG doesn't have a datetimeoffset type - I tend to consider that type more harmful than helpful; it gives people the illusion of being timezone aware, without that being the case (an offset isn't a time zone, no daylight savings awareness).
[...] EF Core 6.0 thankfully did introduce HaveConversion, so I will go with this approach for now.
Yep - one of the main motivators for this feature was bulk configuration of value converters just like this. I'd still recommend doing the conversion earlier - when the DateTimeOffset first enters your application, rather than when you save it to the database.
But the writing change cannot change the fact that no matter what you do at EF Core/.NET, the timestamptz column shall remain what it is – storing an absolute point in time in UTC format. So you can ask us to manage or manipulate our DateTimeOffset in our codes however you think is right, or set up value converters etc, but at the end of the day, as long as we continue using the timestamptz column (and we have no other choice), nothing really changes, right?
You may not care about the DateTimeOffset's offset in your particular case, and treat it basically as a representation of a UTC timestamp (though I think there are potential problems with that approach). Other people may indeed care about the offset - for them, a DateTimeOffset isn't just a UTC timestamp. For these people, the fact that the offset can't be stored is a real problem; you're effectively asking me to get silently, implicitly rid of a piece of information which you don't care about, but which others do.
So that exception, which caused the breaking change, is effectively only informational. There actually isn't anything we can do to "fix" or change this specific PostgreSQL type. That we can and should store time zone separately if needed is true regardless if you throw that exception or not.
I get what you're saying, but consider this... A user could come and ask for the driver to automatically save .NET integers to PostgreSQL text columns. We don't allow that currently, because there's a strong type system in place, and we don't want the ambiguity and fuzziness of implicit conversions and dynamic type systems. That also could be seen as an "informational" exception that just gets in the way, forcing people to convert their ints to text before saving them to the database.
Some other driver or provider could choose to go down this route, and try being "friendly" to users by doing various magic to never raise exceptions. I personally don't believe in this philosophy - strict type systems, explicit conversions and less magic help keep everything very clear and crisp, and users aren't left wondering why a piece of their data suddenly disappeared or got transformed into something else, just because their driver decided to "help out".
One last note... The "standard" representation of a UTC timestamp in .NET is a DateTime with Kind=UTC; this doesn't have all the above issues, doesn't have an offset which you want to ignore anyway, and doesn't need to be converted. Unfortunately that type is awful in many ways, and I do understand why you'd want to avoid it. If all this is really problematic for you, I can only suggest switching to NodaTime where none of these issues exist...
Thanks @roji for the insights. Well 6.0 is still new, but as more people migrate to it, my fear is that the runtime exception can potentially do more harm than good.
If you think about it, this change essentially introduced such a "rule" of yours into .NET that says:
When the type DateTimeOffset is used in an EF Core entity and Npgsql is the provider, be warned that it must have an offset of 0, or else saving changes to DB will throw exception.
The problem is that .NET itself is not aware of such a rule, as this type was inherently designed to hold any offset. This means we will get no compile time warnings of any kind. Isn't that a bit dangerous?
Without a global override, it becomes solely the developers' responsibility to ensure that this rule is honored (again without compiler support), or else runtime exceptions will be thrown.
If we think from a business perspective, saving a time record into DB and unknowingly lose the offset info, vs losing the entire record altogether due to an exception, I'd say the former is less damaging.
And of course, setting the global converter just to be safe = essentially going back to prior behavior.
If you think about it, this change essentially introduced such a "rule" of yours into .NET that says:
There's no ".NET rule" being introduced - your database simply doesn't support non-zero offsets, so Npgsql doesn't allow writing them. There are a million other .NET types which cannot be written to PostgreSQL, and runtime exceptions occur if developers try to write those too. Not every package or database is going to support every data type - that's just the reality of life.
If we think from a business perspective, saving a time record into DB and unknowingly lose the offset info, vs losing the entire record altogether due to an exception, I'd say the former is less damaging.
I couldn't disagree more... If the offset is important and Npgsql converts, then you'd be unknowingly losing data without any warning. In the current situation, you get an informative exception and can deal with the matter as you see fit. You seem to be assuming that everyone out there uses DateTimeOffset in the same way as you do, i.e. as a pure UTC timestamp, where the offset doesn't matter.
This means we will get no compile time warnings of any kind. Isn't that a bit dangerous?
Unfortunately not everything in the world can be done at compile time. Assuming you have very minimal testing which you run after upgrading to Npgsql 6.0, you'll immediately get the informative exception - I think that's pretty safe (if you're deploying to production without testing after having done a major version upgrade, well...). The fact that we're having this conversation itself shows that the exception was effective...
Stepping back, I did consciously decide to err on the "too strict" side with these timestamp changes. Another example of this is that Npgsql doesn't accept UTC timestamps for timestamp without time zone, or non-UTC timestamps for timestamp with time zone. I could have just allowed them; for example, these restrictions "needlessly" force people to change DateTime.Now into DateTime.UtcNow when inserting into timestamp with time zone (the default). The whole timestamp/time zone situation is very tricky, and the BCL types make it even easier to get confused and do things wrong. The strictness forces users to pause and think about what they're doing: do I actually need the offset in DateTimeOffset? Should I just convert to a UTC DateTime, or get rid of the offset eariler? These questions are important, and the strictness contributes to users giving them some thought.
When making these far-reaching changes, I was afraid a lot more of users would complain about the breaks - that actually hasn't been the case at all... I've even gotten some appreciative remarks on how the new behavior forces cleaner code.
Activity
roji commentedon Nov 19, 2021
First, I'd recommend giving this blog post which contains a lot of the reasoning behind all these changes.
Quite a few things... First, many users writing DateTimeOffset to PostgreSQL expected the offset to be saved. That's a very reasonable expectation, especially considering that SQL Server (which many people come from) does have a datetimeoffset type; this caused a lot of confusion. The exception lets people know up-front that it's not possible to fully and faithfully represent the type in the database.
Related to the above, the conversions made things even more confusing, since DateTimeOffsets wouldn't be roundtripped: you write a non-UTC DateTimeOffset, but when you read back the same thing, you get a UTC DateTimeOffset. It's also not possible to convert symmetrically: when writing, we can convert non-UTC to UTC, but we can't convert back when reading (previous versions actually converted to the local timezone of the machine, which is an even worse idea...).
In short, I believe a database driver really should faithfully read and write your data to the database, and not perform implicit conversions on it; any under-the-hood magic here is harmful and makes things harder to understand.
It's all the difference in the world - when the developer makes a conscience decision to convert, they're aware of it and are explicitly opting into it.
There's really no need for that - if you want to systematically convert all DateTimeOffsets prior to sending, simply set up an EF Core value converter once, and you're done. Although I'd argue that if you're really doing UTC everywhere, then you shouldn't even have a non-UTC DateTimeOffset coming anywhere near EF Core - you should be converting to UTC much earlier, at the edges when reading the value.
Re adding another behavior switch for this... Every switch increases complexity (code maintenance, testing, user bug report analysis) so I generally don't like to do that unless there's ample user need. We can see whether other users run into this, but given that there's an easy workaround (value converters) I don't think it's warranted at this point.
felinepc commentedon Nov 20, 2021
@roji Thanks for the thorough explanation on the history behind these changes. The root of all the mess was pretty much the fact that PostgreSQL simply doesn't have a
DateTimeOffsettype, and yet they somehow chose to name theirtimestamptztype in a misleading way to make it sound like they do. 👿I forgot about using value converters on a built-in type (just didn't have to do it before). Also my first thought after your post was "but don't I still have to add the converter to every
DateTimeOffsetproperty in every entity class...", but then found that EF Core 6.0 thankfully did introduceHaveConversion, so I will go with this approach for now.But to circle back to the original point about this breaking change...
I can totally see how the reading behavior changes were totally justified. As you said, things like using local machine timezone etc were obviously bad ideas. And those changes actually have effects.
But the writing change cannot change the fact that no matter what you do at EF Core/.NET, the
timestamptzcolumn shall remain what it is – storing an absolute point in time in UTC format. So you can ask us to manage or manipulate ourDateTimeOffsetin our codes however you think is right, or set up value converters etc, but at the end of the day, as long as we continue using thetimestamptzcolumn (and we have no other choice), nothing really changes, right?So that exception, which caused the breaking change, is effectively only informational. There actually isn't anything we can do to "fix" or change this specific PostgreSQL type. That we can and should store time zone separately if needed is true regardless if you throw that exception or not.
All the codes we need to add/change (like in my case, the value converter), only served one purpose: to satisfy this breaking change itself so that we don't get these runtime exceptions.
So isn't it more suitable to make this some warning that can be acknowledged and suppressed, rather than a runtime exception?
roji commentedon Nov 20, 2021
It indeed partially has to do with this... FWIW apparently the timestamptz type (and name) is specified by the SQL standard, rather than a PG invention. Also, I'm personally happy PG doesn't have a datetimeoffset type - I tend to consider that type more harmful than helpful; it gives people the illusion of being timezone aware, without that being the case (an offset isn't a time zone, no daylight savings awareness).
Yep - one of the main motivators for this feature was bulk configuration of value converters just like this. I'd still recommend doing the conversion earlier - when the DateTimeOffset first enters your application, rather than when you save it to the database.
You may not care about the DateTimeOffset's offset in your particular case, and treat it basically as a representation of a UTC timestamp (though I think there are potential problems with that approach). Other people may indeed care about the offset - for them, a DateTimeOffset isn't just a UTC timestamp. For these people, the fact that the offset can't be stored is a real problem; you're effectively asking me to get silently, implicitly rid of a piece of information which you don't care about, but which others do.
I get what you're saying, but consider this... A user could come and ask for the driver to automatically save .NET integers to PostgreSQL text columns. We don't allow that currently, because there's a strong type system in place, and we don't want the ambiguity and fuzziness of implicit conversions and dynamic type systems. That also could be seen as an "informational" exception that just gets in the way, forcing people to convert their ints to text before saving them to the database.
Some other driver or provider could choose to go down this route, and try being "friendly" to users by doing various magic to never raise exceptions. I personally don't believe in this philosophy - strict type systems, explicit conversions and less magic help keep everything very clear and crisp, and users aren't left wondering why a piece of their data suddenly disappeared or got transformed into something else, just because their driver decided to "help out".
roji commentedon Nov 20, 2021
One last note... The "standard" representation of a UTC timestamp in .NET is a DateTime with Kind=UTC; this doesn't have all the above issues, doesn't have an offset which you want to ignore anyway, and doesn't need to be converted. Unfortunately that type is awful in many ways, and I do understand why you'd want to avoid it. If all this is really problematic for you, I can only suggest switching to NodaTime where none of these issues exist...
mikequ-taggysoft commentedon Nov 20, 2021
Thanks @roji for the insights. Well 6.0 is still new, but as more people migrate to it, my fear is that the runtime exception can potentially do more harm than good.
If you think about it, this change essentially introduced such a "rule" of yours into .NET that says:
When the type
DateTimeOffsetis used in an EF Core entity and Npgsql is the provider, be warned that it must have an offset of 0, or else saving changes to DB will throw exception.The problem is that .NET itself is not aware of such a rule, as this type was inherently designed to hold any offset. This means we will get no compile time warnings of any kind. Isn't that a bit dangerous?
Without a global override, it becomes solely the developers' responsibility to ensure that this rule is honored (again without compiler support), or else runtime exceptions will be thrown.
If we think from a business perspective, saving a time record into DB and unknowingly lose the offset info, vs losing the entire record altogether due to an exception, I'd say the former is less damaging.
And of course, setting the global converter just to be safe = essentially going back to prior behavior.
I guess only time will tell...
roji commentedon Nov 21, 2021
There's no ".NET rule" being introduced - your database simply doesn't support non-zero offsets, so Npgsql doesn't allow writing them. There are a million other .NET types which cannot be written to PostgreSQL, and runtime exceptions occur if developers try to write those too. Not every package or database is going to support every data type - that's just the reality of life.
I couldn't disagree more... If the offset is important and Npgsql converts, then you'd be unknowingly losing data without any warning. In the current situation, you get an informative exception and can deal with the matter as you see fit. You seem to be assuming that everyone out there uses DateTimeOffset in the same way as you do, i.e. as a pure UTC timestamp, where the offset doesn't matter.
Unfortunately not everything in the world can be done at compile time. Assuming you have very minimal testing which you run after upgrading to Npgsql 6.0, you'll immediately get the informative exception - I think that's pretty safe (if you're deploying to production without testing after having done a major version upgrade, well...). The fact that we're having this conversation itself shows that the exception was effective...
Stepping back, I did consciously decide to err on the "too strict" side with these timestamp changes. Another example of this is that Npgsql doesn't accept UTC timestamps for
timestamp without time zone, or non-UTC timestamps fortimestamp with time zone. I could have just allowed them; for example, these restrictions "needlessly" force people to change DateTime.Now into DateTime.UtcNow when inserting intotimestamp with time zone(the default). The whole timestamp/time zone situation is very tricky, and the BCL types make it even easier to get confused and do things wrong. The strictness forces users to pause and think about what they're doing: do I actually need the offset in DateTimeOffset? Should I just convert to a UTC DateTime, or get rid of the offset eariler? These questions are important, and the strictness contributes to users giving them some thought.When making these far-reaching changes, I was afraid a lot more of users would complain about the breaks - that actually hasn't been the case at all... I've even gotten some appreciative remarks on how the new behavior forces cleaner code.