How to return custom table types from Npgsql and stored procedures?

后端 未结 1 1191
南旧
南旧 2021-01-14 10:48

I\'m trying to return a custom (composite) type based on an implicit table type.

I have this table definition:

CREATE TABLE app_user (id CHAR(36) PRI         


        
相关标签:
1条回答
  • 2021-01-14 11:31

    Looks like I figured it out. Turned out to be easier than I thought. All I needed to change was the way the stored procedure was called from C#.

    ApplicationUser user;
    using (NpgsqlConnection db = new NpgsqlConnection(this.connectionString))
    {
        db.Open();
        using (NpgsqlCommand cmd = new NpgsqlCommand("SELECT find_by_id(@user_id);", db))
        {
            cmd.Parameters.AddWithValue("user_id", userId);
            object result = cmd.ExecuteScalar();
            user = result == DBNull.Value ? null : (ApplicationUser)result;
        }
    }
    

    I preferred the other way of invoking the stored procedure, but at least this works!

    0 讨论(0)
提交回复
热议问题