Home > Software design >  How to get error sql from PostgresException
How to get error sql from PostgresException

Time:12-19

ASP.NET Core 6 MVC application uses EF Core and Npgsql.

Ef Core command like

await ctx.Kasutajas.Where((k) => k.Kasutaja1 == suurusername).SingleOrDefaultAsync();

throws PostgresException in case of missing column. BatchCommand property in null. It looks like EF Core does not use batch command interface. How to get error sql in this case?

   app.Use(async (context, next) =>
            {
                try
                {
                    await next.Invoke();
                }
                catch (PostgresException ex)
                {
ex.BatchCommand in null.
ex.InternalQuery is null.
            Debug.WriteLine(ex.InternalQuery);
?? How to get error Sql ?
                }
                }
            });

In .NET 5 Ngpsql had statement property for this but it was removed according to

https://www.npgsql.org/doc/release-notes/6.0.html#npgsqlstatement-and-postgresexceptionstatement-have-been-removed

How to get error SQL in CATCH block in exception handler? Discussion in https://github.com/npgsql/npgsql/issues/4222 does not provide solution.

CodePudding user response:

PostgresException.BatchCommand support is being added for regular commands, like the PostgresException.Statement in pre-6.0 Npgsql. See https://github.com/npgsql/npgsql/issues/4222.

  • Related