npgsql / EntityFramework6.Npgsql

Entity Framework 6 provider for PostgreSQL
PostgreSQL License
66 stars 53 forks source link

Using NodaTime in EF Core yield InvalidCastException in EF6 #181

Open Bouke opened 3 years ago

Bouke commented 3 years ago

I'm writing an application that's in the process of migrating to .NET 5 / Core / EF Core. However as most of the projects are targeting .NET Framework, this change will take some time. In the interim, we're writing new projects in EF Core 3.1, while the existing projects continue to work on EF6.

NodaTime isn't being used for the entities in the EF6 project (just DateTimeOffset / DateTime). However after adding NodaTime to the EFCore project, querying EF6 yields a InvalidCastException on the following lines: https://github.com/npgsql/npgsql/blob/613f095dede08f89a596c4360a6f3d8477050688/src/Npgsql/NpgsqlDataReader.cs#L1641-L1643.

image

roji commented 3 years ago

Which version of Npgsql are you using (the ADO, not EF Core provider)? This sounds like https://github.com/npgsql/npgsql/issues/2439 which was fixed in Npgsql 4.1.5

Bouke commented 3 years ago

According to my lock file and file explorer, I'm on version 4.1.8. Interestingly the problem only surfaces when I actually model my EF Core class to use a Nodatime type.

roji commented 3 years ago

Can you please submit a minimal code sample, i.e. a small project with both EF Core and EF6 in use, which reproduces this problem?

Bouke commented 3 years ago

@roji Sure! Npgsql1704.zip

roji commented 3 years ago

OK, I can see the problem now - EF6 uses a special legacy mechanism to read values from NpgsqlDataReader. Instead of using generic reader.GetFieldValue<DateTime>, it calls the non-generic GetValue() but uses ObjectResultTypes to inform the reader in advance of the types it will want to read back. This mechanism doesn't work for this date/time case, since the NodaTime plugin has already ready an Instant, which can't be cast/converted to a DateTime.

While it may be possible to continue hacking the ObjectResultTypes mechanism to make EF6 work, the EF6 provider is now archived and not receiving significant attention - so we probably won't be able to fix this. If you must use both EF6 and EF Core, I'd recommend not using the NodaTime plugin for now, until the entire application can be moved to EF Core.