Open kolev9605 opened 4 months ago
The type system in EF6 is extremely limited and has more or less no options for such customizations. I suggest you to use EF Core instead on .NET 8.
The type system in EF6 is extremely limited and has more or less no options for such customizations. I suggest you to use EF Core instead on .NET 8.
The other service is .NET Framework, so using EF Core is not an option. I will state that more explicitly in the post.
The problem really is how do I store proper UTC timestamp everywhere in the database and at the same time make both services (one is .NET 8 and the other is .NET Framework 4.8) to work properly with the timestamps in the DB.
I would in that case suggest you to have some middle layer where you convert a DateTimeOffset when reading the DB into a DateTime and the other way when you write to the DB. Unless you want to modify the source code of this repo.
The type system in EF6 is not flexible at all. It's designed to work with SQL Server. PostgreSQL has a different set of date/time types which is not really 100% compatible. A different issue with the DateTime type in dotnet is that there is an associated Kind with every value (Local, UTC or Unknown) which complicates it further.
I would in that case suggest you to have some middle layer where you convert a DateTimeOffset when reading the DB into a DateTime and the other way when you write to the DB. Unless you want to modify the source code of this repo.
The type system in EF6 is not flexible at all. It's designed to work with SQL Server. PostgreSQL has a different set of date/time types which is not really 100% compatible. A different issue with the DateTime type in dotnet is that there is an associated Kind with every value (Local, UTC or Unknown) which complicates it further.
So far the only solution I have is to store timestamp without timezone
in the database and have interceptors in the application setting the Kind to UTC. This is working, but I wanted to use Npgsql
as intended, that's why I am asking is there any solution to this case.
some middle layer where you convert a DateTimeOffset when reading the DB into a DateTime and the other way when you write to the DB
How would you achieve that?
The intended use in EF6 when you want to store a time stamp that specifies a specific point in time is to use DateTimeOffset rather than a DateTime with UTC as kind. The "offset" here is the offset from UTC+0. "timestamp without time zone" is mapped to DateTime (therefore with an unknown offset).
With "some middle layer", that would be an "interceptor" or something in your application code that converts between the desired types, outside of EF. From your description it seems you have structured your application in a modular way where it seems this would be possible, as you say you can already set the Kind to UTC.
So, the only way I managed to make both .NET Framework and .NET 8 to work with the same DB is the following:
timestamp without timezone
Kind.Utc
to all timestamps fetched from the DBAppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
DateTime
is used in both applications instead of DateTimeOffset
for timestamps fetched from the DB.
We are doing a PostgreSQL migration to our codebase. One of our services is not yet migrated to .NET 8 but it is using the same DB as the other services which are already migrated to .NET 8.
The problem we are facing is the following:
Let's take a look first at the .NET 8 setup:
Service 1, .NET 8, EF Core 8.
Versions:
DB Context & entity:
When I generate a migration, it works fine and the type chosen is
timestamp with time zone
. From that point onward, read/write operations work fine, and I am able to fetch a UTC timestamp into aDateTime
property.In .NET Framework, though, it is not like that.
Service 2, .NET Framework 4.8, EF6.
Versions:
DB Context & Entity:
If I try to add a migration here, though, I get the following error:
It works fine if I change the type of
SomeDate
toDateTimeOffset
.I understand (kind of) why it works like that reading the breaking changes with version 6.0 of Npgsql, but is there something we can do to force EF to map the
timestamptz
columns toDateTime
properties?