EntityFramework6.Npgsql icon indicating copy to clipboard operation
EntityFramework6.Npgsql copied to clipboard

Using NodaTime in EF Core yield InvalidCastException in EF6

Open Bouke opened this issue 4 years ago • 5 comments

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

Bouke avatar Feb 16 '21 06:02 Bouke

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

roji avatar Feb 16 '21 08:02 roji

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.

Bouke avatar Feb 16 '21 10:02 Bouke

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?

roji avatar Feb 16 '21 11:02 roji

@roji Sure! Npgsql1704.zip

Bouke avatar Feb 23 '21 09:02 Bouke

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.

roji avatar Mar 09 '21 10:03 roji