EntityFramework-Plus icon indicating copy to clipboard operation
EntityFramework-Plus copied to clipboard

Unable to cast object of type 'Z.EntityFramework.Plus.CreateEntityConnection' to type 'Npgsql.NpgsqlConnection'.

Open tomerpeled opened this issue 3 years ago • 11 comments

After upgrading to .NET Core 6 (with EFCore 6 update + EntityFramework-plus), I'm now getting the following exception when using the Plus lib: System.InvalidCastException: Unable to cast object of type 'Z.EntityFramework.Plus.CreateEntityConnection' to type 'Npgsql.NpgsqlConnection'. at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlRelationalConnection.get_DbConnection() at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlRelationalConnection.get_CurrentAmbientTransaction() at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.HandleAmbientTransactions() at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.Enumerator.InitializeReader(Enumerator enumerator) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.Enumerator.<>c.<MoveNext>b__19_0(DbContext _, Enumerator enumerator) at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.Enumerator.MoveNext() at Z.EntityFramework.Plus.QueryFutureValue1.SetResult(DbDataReader reader) at Z.EntityFramework.Plus.QueryFutureBatch.ExecuteQueriesAsync(CancellationToken cancellationToken) at Z.EntityFramework.Plus.QueryFutureValue`1.ValueAsync(CancellationToken cancellationToken)

Any idea how can we solve it?

Thanks, Tomer

tomerpeled avatar Nov 18 '21 07:11 tomerpeled

Hello @tomerpeled ,

Thank you for reporting, we will try to reproduce it.

Best Regards,

Jon


Sponsorship Help us improve this library

Performance Libraries context.BulkInsert(list, options => options.BatchSize = 1000); Entity Framework ExtensionsBulk OperationsDapper Plus

Runtime Evaluation Eval.Execute("x + y", new {x = 1, y = 2}); // return 3 C# Eval FunctionSQL Eval Function

JonathanMagnan avatar Nov 18 '21 16:11 JonathanMagnan

Hello @tomerpeled ,

Thank again for reporting.

Unfortunately, it looks like that PostgreSQL started to cast the connection. So the trick we used to make this feature work doesn't longer work as it requires the same exact connection type as the original.

Unfortunately, we currently have no solution for this issue.

We will try to eventually look again at it but for now, we will consider this feature do not longer supports this provider as this is impossible for us to fix it with the current time we have.

JonathanMagnan avatar Nov 22 '21 17:11 JonathanMagnan

Hi @JonathanMagnan,

Thanks for the update! Understood, we will probably hold the. Net core upgrade for now, because we prefer the Futures feature above the new .Net core features :)

tomerpeled avatar Nov 22 '21 19:11 tomerpeled

Hello, @tomerpeled !

Did you find the alternative to Future queries? I also need them to implement total in pagination use case.

https://entityframework-plus.net/ef-core-query-future#paging

pandarun avatar Dec 13 '21 15:12 pandarun

Hi @pandarun,

Unfortunately, there is no workaround for now...

tomerpeled avatar Dec 13 '21 19:12 tomerpeled

@JonathanMagnan do you think raising issue about this in Npgsql repo may help?

alexb5dh avatar Feb 11 '22 02:02 alexb5dh

Hello @alexb5dh ,

Unfortunately no, the error happens here if I remind correctly: https://github.com/npgsql/efcore.pg/blob/cf4f23ecc271b3c9753600ec84a65c7d4053991d/src/EFCore.PG/Storage/Internal/NpgsqlRelationalConnection.cs#L81

It cast our custom connection into a NpgsqlConnection. We did too much internal "hack" on our side and the only good way to make everything work again is probably to fully re-write this feature. Unfortunately, it is hard to find enough time for doing it.

JonathanMagnan avatar Feb 11 '22 14:02 JonathanMagnan

Hi,

I have the same issue with .net6 upgrade. Are you working on this problem? Will we be able to upgrade to net6 sometime?

Thx, Andrew

Balogh-Andras avatar Jun 23 '22 10:06 Balogh-Andras

Hello @Balogh-Andras ,

Unfortunately, we don't have enough time at this moment to work on this problem.

Supporting EF Core 7 and re-writing our core code to support new features they bring is currently our priority.

We hope to eventually fix it, but like everyone's problem, time is missing, so we currently prioritize development. This one is not easy to fix and ask us to rewrite most of it to support PostgreSQL and Oracle.

JonathanMagnan avatar Jun 23 '22 13:06 JonathanMagnan

Maybe a way to fix this is to have a cast operator for the original connection type ? There would be a add-on for each EFCoreProvider that would provide the CreateEntityConnection with the needed cast operator.

I'm really new to efcore plus as I've just downloaded it a few minutes ago.

kYann avatar Aug 14 '22 14:08 kYann

After updating my project to .NET 7, EF7, Npgsql 7, and EntityFramework-Plus' last version, I don't get this Exception anymore and it seems to be working correctly. Give a try!

joaofx avatar Dec 16 '22 17:12 joaofx