Search code examples
entity-framework-corenpgsql

How to call system function with entity framework core for postgreSQL


There is a postgreSQL function "pg_try_advisory_lock(key bigint)" that returns bool result. Is there any solution how can i execute this function and get result via EF Core?


Solution

  • EF Core doesn't currently support running queries which return primitive (non-entity) types, #11624 tracks that.

    However, it's very easy to drop down to ADO.NET and execute your function:

    await ctx.Database.OpenConnectionAsync();
    using var conn = ctx.Database.GetDbConnection();
    var cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT pg_try_advisory_lock(8)";
    var result = (bool)await cmd.ExecuteScalarAsync();
    await ctx.Database.CloseConnectionAsync();