You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using NodaTime in EF Core yield InvalidCastException in EF6 #181
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.
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.
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.
Activity
roji commentedon Feb 16, 2021
Which version of Npgsql are you using (the ADO, not EF Core provider)? This sounds like npgsql/npgsql#2439 which was fixed in Npgsql 4.1.5
Bouke commentedon Feb 16, 2021
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 commentedon Feb 16, 2021
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 commentedon Feb 23, 2021
@roji Sure! Npgsql1704.zip
roji commentedon Mar 9, 2021
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-genericGetValue()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.