EntityFramework-Plus
EntityFramework-Plus copied to clipboard
Unable to cast object of type 'Z.EntityFramework.Plus.CreateEntityConnection' to type 'Npgsql.NpgsqlConnection'.
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.SingleQueryingEnumerable
1.Enumerator.<>c.<MoveNext>b__19_0(DbContext _, Enumerator enumerator)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func
3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.Enumerator.MoveNext() at Z.EntityFramework.Plus.QueryFutureValue
1.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
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 Extensions • Bulk Operations • Dapper Plus
Runtime Evaluation
Eval.Execute("x + y", new {x = 1, y = 2}); // return 3
C# Eval Function • SQL Eval Function
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.
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 :)
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
Hi @pandarun,
Unfortunately, there is no workaround for now...
@JonathanMagnan do you think raising issue about this in Npgsql repo may help?
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.
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
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.
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.
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!