1

I'm getting the following error trying to load the results of my Postgres function into a EF entity.

Error:

Microsoft.EntityFrameworkCore.Query: Error: An exception occurred while iterating over the results of a query for context type 'Rainman.Data.RainmanDbContext'.
System.Threading.ThreadAbortException: System error.
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.Enumerator.MoveNext()

While running the following code:

    FormattableString query = $"SELECT sortedsetkey as \"SortedSetKey\", keyorder as \"KeyOrder\", value as \"Value\" FROM public.fn_test_lcr_custom({routePlan.DeckApplyCustomer.tg_id}, {routePlan.effective_date});";
    Console.WriteLine($"\n\nQUERY:\n{query}\n\n");
    IEnumerable <CustomerRoutingCache> tmpList = RainmanDbContext.CustomerRoutingCaches.FromSqlInterpolated(query).AsEnumerable();

This SQL Function Definition for the function being called on the Postgres side (ver 13):

CREATE OR REPLACE FUNCTION public.fn_test_lcr_custom(
    p_tg_id text,
    p_as_of_date timestamp with time zone)
    RETURNS TABLE(SortedSetKey text, KeyOrder int, Value text) 

The class that it being translated into is:

public class CustomerRoutingCache
{
    [Key]
    public string SortedSetKey { get; set; }
    public int KeyOrder { get; set; }
    public string Value { get; set; }
}

Using .net core 5, npgsql

Sam Ware
  • 131
  • 9
  • Examine your code for `Thread.Abort()` function call. When you found it, try to remove that and replace with more safe approach. – Svyatoslav Danyliv Aug 02 '21 at 21:53
  • I searched all the files in my solution and I don't have a Thread.Abort() function call; unless, it is in one the external libraries that I am using. – Sam Ware Aug 03 '21 at 02:35
  • Well, this exception has nothing with EF Core or Postgres. Someone called Thread.Abort. – Svyatoslav Danyliv Aug 03 '21 at 05:18
  • I have code (many times at this point) to try and resolve the issue. Now I am getting a "System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. ---> Npgsql.NpgsqlException (0x80004005): Exception while reading from stream ---> System.TimeoutException: Timeout during reading attempt" I am turning off "Just My Code" to see if I can determine what could be the cause for the various errors. – Sam Ware Aug 03 '21 at 05:53
  • @SamWare TimeoutException are TheradAbortException are very different things - you need to clarify exactly what you're getting. The TheradAbortException may be because IIS (or someone else) is shutting down your application or similar - there's nothing Npgsql or EF Core can do about that. – Shay Rojansky Aug 18 '21 at 14:00

0 Answers0