79526156

Date: 2025-03-21 17:13:13
Score: 1
Natty:
Report link

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$;
Reasons:
  • RegEx Blacklisted phrase (1.5): I'm new
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Илья Ступин