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

Add DateOnly/TimeOnly to What's New in EF8 #4245

Merged
merged 2 commits into from
Feb 10, 2023

Conversation

ajcvickers
Copy link
Contributor

No description provided.

@ajcvickers ajcvickers force-pushed the TimeGotTheTimeTickTickTickinInMyHead0202 branch from 6d4fdeb to 5b7dc13 Compare February 7, 2023 20:54
@ajcvickers ajcvickers force-pushed the TimeGotTheTimeTickTickTickinInMyHead0202 branch from 5b7dc13 to 00780d8 Compare February 7, 2023 20:59
@ajcvickers ajcvickers requested a review from a team February 7, 2023 21:00
### DateOnly/TimeOnly supported on SQL Server

The <xref:System.DateOnly> and <xref:System.TimeOnly> types were introduced in .NET 6 and have been supported for several database providers (e.g. SQLite, MySQL, and PostgreSQL) since their introduction. For SQL Server, the recent release of a [Microsoft.Data.SqlClient](https://www.nuget.org/packages/Microsoft.Data.SqlClient/) package targeting .NET 6 has allowed [ErikEJ to add support for these types at the ADO.NET level](https://github.com/dotnet/SqlClient/pull/1813). This in turn paved the way for support in EF8 for `DateOnly` and `TimeOnly` as properties in entity types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention support is also available for EF Core 6/7 ? 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, a note seems to be a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

> The code shown here comes from [DateOnlyTimeOnlySample.cs](https://github.com/dotnet/EntityFramework.Docs/tree/main/samples/core/Miscellaneous/NewInEFCore8/DateOnlyTimeOnlySample.cs).

> [!NOTE]
> This model represents only British schools and stores times as local (GMT) times. Handling different timezones would complicate this code significantly. Note that using `DateTimeOffset` would not help here, since opening and closing times have different offsets depending whether daylight saving time is active or not.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW unless I'm mistaken, this could be made international simply by introducing a timezone column in the table, so that we know what timezone each school is in. Then, the e.g. opening hour is always interpreted with respect to that timezone. But I agree it's not worth going into that here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the recommended way of writing queries against that? Presumably, I would use a UTC time as the parameter, but then convert the column value to UTC in the query? How would I do that conversion--is there a .NET BCL way of doing, or do I need to use a database-specific function? Also, the conversion will be dependent on the day for which the query is running, since it will give a different UTC depending on daylight saving or not. And finally, I would need to be sure that overflow to the next/previous day didn't return incorrect results.

I gave up! :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahahaha yeah time zone stuff is always awesome :)

So that's why we added EF.Functions.AtTimeZone for SQL Server. The PG provider does translate various BCL APIs because they're a good fit (see these docs, scroll down a bit).

But you're right that you can't do time zone conversion on just a time - you need a full timestamp (8:00AM converts to different times in other time zones e.g. based on daylight savings). So if the goal is e.g. to get a UTC time for the school opening (for whatever reason), the query would need to construct a timestamp from the database time - providing some date as appropriate - convert that to UTC (with AtTimeZone), and then extract the time component out of that. I think we should have all the building blocks for that, but I'm not 100% sure.

(either way, I definitely think we\ don't need to go into all this complexity in an intro to DateOnly/TimeOnly!).

### DateOnly/TimeOnly supported on SQL Server

The <xref:System.DateOnly> and <xref:System.TimeOnly> types were introduced in .NET 6 and have been supported for several database providers (e.g. SQLite, MySQL, and PostgreSQL) since their introduction. For SQL Server, the recent release of a [Microsoft.Data.SqlClient](https://www.nuget.org/packages/Microsoft.Data.SqlClient/) package targeting .NET 6 has allowed [ErikEJ to add support for these types at the ADO.NET level](https://github.com/dotnet/SqlClient/pull/1813). This in turn paved the way for support in EF8 for `DateOnly` and `TimeOnly` as properties in entity types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, a note seems to be a good idea.

@ajcvickers ajcvickers merged commit 7886a9d into main Feb 10, 2023
@ajcvickers ajcvickers deleted the TimeGotTheTimeTickTickTickinInMyHead0202 branch February 10, 2023 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants