I spent a lot of time and decided to share the solution.
And I found this to be the best solution for me.
public class SomeDbContext :DbContext
{
...
[DbFunction("set_and_get_upp_user_instance", "public")]
public IQueryable<AppUser> SetAndGetUppUserInstance(Guid userId) => FromExpression(() => this.SetAndGetUppUserInstance(userId));
...
}
var appUser = await this._context.SetAndGetUppUserInstance(userId).FirstOrDefaultAsync(c => c.UserId == userId);
Apologies for postgresql, I'm new to this.
CREATE OR REPLACE FUNCTION set_and_get_upp_user_instance(p_user_id uuid)
RETURNS TABLE (
AppUserID uuid,
UserID uuid,
LastIPAddress character varying(64),
IsOnline BOOLEAN,
LastConnected TIMESTAMPTZ,
LastDisconected TIMESTAMPTZ,
CurrentLanguageID uuid
)
LANGUAGE plpgsql
AS $function$
BEGIN
RETURN QUERY SELECT * FROM get_app_user_by_id(p_user_id);
IF NOT FOUND THEN
INSERT INTO "AppUser" (UserID, LastIPAddress, IsOnline, CurrentLanguageID)
VALUES (p_user_id, '124', true, get_default_language());
END IF;
RETURN QUERY SELECT * FROM get_app_user_by_id(p_user_id);
END;
$function$;