Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using NodaTime in EF Core yield InvalidCastException in EF6 #181

Open
Bouke opened this issue Feb 16, 2021 · 5 comments
Open

Using NodaTime in EF Core yield InvalidCastException in EF6 #181

Bouke opened this issue Feb 16, 2021 · 5 comments

Comments

@Bouke
Copy link

Bouke commented Feb 16, 2021

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
Copy link
Member

roji commented 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
Copy link
Author

Bouke commented 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
Copy link
Member

roji commented 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
Copy link
Author

Bouke commented Feb 23, 2021

@roji Sure! Npgsql1704.zip

@roji
Copy link
Member

roji commented 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-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 roji transferred this issue from npgsql/efcore.pg Dec 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants