Comments (7)
Microsoft recommends using the DateTimeOffset rather than the DateTime.
Can you point to that recommendation? I certainly think it should be true for all cases.
It would be great to be able to use the Npgsql.NpgsqlCommand.ExecuteReaderAsync() method to get a more accurate CLR/Type when using timestamp with time zone Postgres column type.
I'm having trouble understanding what exactly you're asking for... A new type to represent PG timestamp with time zone
?
from efcore.pg.
Microsoft recommends using the DateTimeOffset](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset?view=net-8.0) rather than the DateTime.
Can you point to that recommendation? I certainly think it should be true for all cases.
See here "consider DateTimeOffset as the default date and time type..."
It would be great to be able to use the Npgsql.NpgsqlCommand.ExecuteReaderAsync() method to get a more accurate CLR/Type when using timestamp with time zone Postgres column type.
I'm having trouble understanding what exactly you're asking for... A new type to represent PG
timestamp with time zone
?
If we were to call ExecuteReaderAsync() on a table with a timestamp with time zone column, the type of the column returned would yield DateTime - rather than DateTimeOffset as one would expect.
using var conn = new NpgsqlConnection("...");
using cmd = conn.CreateCommand();
cmd.cmd.CommandText = "SELECT datetime1 FROM table1";
conn.Open();
var rdr = await cmd.ExecuteReaderAsync();
while (rdr.Read()) {
Debug.Write(rdr.GetValue(0).GetType()); // <- this returns DateTime, NOT DateTimeOffset
}
conn.Close();
I haven't tried with DateOnly but assume it too would use CLR DateTime.
from efcore.pg.
Here is nice article about CLR date-time. https://medium.com/@gsferreira/datetimeoffset-vs-datetime-whats-the-difference-2183da605765
from efcore.pg.
See here "consider DateTimeOffset as the default date and time type..."
For representing UTC timestamps, I'm not sure I'd necessarily choose DateTimeOffset with offset 0 over DateTime with Kind=Utc.
But more to the point, when using ADO.NET, it's best practice to use the generic GetFieldValue<T>()
instead of GetValue()
wherever possible; this is is the case in 99% per of scenarios, where the expected type is known. The exception is when doing dynamic programming, where unknown SQL is being executed against an unknonw schema, and the actual types are unknown. This is one reason why the "default" isn't extremely important for most applications.
In any case, changing the default would be a significant breaking changes, and I definitely don't feel that returning DateTimeOffset is that much more "correct" than DateTime, so as to justify something like this.
from efcore.pg.
- My case is generic program which doesn't know the expected type in advance.
- Either way, would GetFIeldValue<DateTimeOffset> work or just try to cast an already converted DateTime?
- What about Date -> DateOnly, Time -> TimeOnly mapping? would those work?
It's preferable in my view to align with Microsoft recommendations.
from efcore.pg.
Either way, would GetFIeldValue work or just try to cast an already converted DateTime?
Yes, it would work. I'm not sure what difference it would make if it "cast an already converted DateTime".
What about Date -> DateOnly, Time -> TimeOnly mapping? would those work?
Yes - it's really easy to just try this and see.
from efcore.pg.
I'm going to go ahead and close this as we won't be changing the default, and the other questions have been answered. But if you have any other questions, feel free to post back here.
from efcore.pg.
Related Issues (20)
- Fully support extensions create in non-default schemas
- Make the enumName parameter in NpgsqlDbContextOptionsBuilder.MapEnum<T>() optional. HOT 7
- How to prefix an update statement with a SET, whilst maintaining DbConcurrencyException logic HOT 3
- All migrations detected as Pending if you don't have a versioned public schema.
- EntityTypeBuilder.HasCluster(string indexName) HOT 1
- Remove-Migration throws System.NullReferenceException when using NpgsqlRange<DateTimeOffset>
- Drop custom collation in EF migration rollback
- Translate range construction, e.g. NpgsqlRange<DateTime> to daterange() HOT 2
- Updating a datetime property through ExecuteUpdateAsync() forces the datetime to be UTC? HOT 4
- Look into TimescaleDB support
- Method 'get_ConstructorExpression' in type 'JsonBigIntegerReaderWriter' from assembly 'Npgsql.EntityFrameworkCore.PostgreSQL' does not have an implementation. HOT 7
- Slow performance on string contains expression. HOT 23
- Error while Querying scalar with context.Database function HOT 4
- ENUMs don't use fully qualified namespaces in ModelSnapshot which causes collisions HOT 2
- Plans for Entity Framework 9 (EF9) support? HOT 2
- Question: Correct way to rotate database credentials when using a datasource HOT 5
- Additional DateOnly related translation
- Missing checks for predicate when simplifying primitive collections
- Translation error when storing NodaTime `(Instant, TimeZoneId)` and filtering on local date time values HOT 1
- Add support for foreign key match types (FULL, PARTIAL, SIMPLE) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from efcore.pg.