1

I am trying to call a Postgresql stored procedure written in plpgsql using C# and Dapper, but I have found that Dapper alphabetizes the parameters before inserting them into the stored procedure. Is there a way I can avoid this behavior and have the items inserted in the correct order?

For example, the following call would not run correctly because it would alphabetize the parameter names. I have to manually alphabetize them for the call to go through.

int map_id = conn.Query<int>(
"insert_color",
new
{
    zebra_name = new DbString { Value = slide_name },
    door_name = new DbString { Value = fov_name },
    foo_number = cycle_number,
    variable_path = new DbString { Value = image_path },
    random_path = new DbString { Value = meta_path },
    x = x,
    y = y,
    z = z,
    exposure = exposure,
    type_name = new DbString { Value = image_type },
    copy = copy
},
commandType: CommandType.StoredProcedure).First();

Here is the stored procedure declaration:

CREATE OR REPLACE FUNCTION insert_color(
zebra_name text, door_name text, foo_number integer,    
variable_path text, random_path text, 
x real, y real, z real, exposure real,
type_nametext, copy integer) RETURNS integer AS $$
    ...
$$ LANGUAGE plpgsql;
5
  • What do you mean by "alphabetize"? Commented Jul 31, 2012 at 19:54
  • It means that instead of sending the parameters to the function in order (zebra name first, then door_name, etc) it sends them alphabetized (copy first, door_name, exposure... etc). I have to add "a_", "b_", "c_" in front of the parameter names for the function to run correctly. Commented Jul 31, 2012 at 20:05
  • Well, in that case, whatever "dapper" is, it's broken. Anything that broken I'd just stop using, but you could try reporting a bug. Of course if it can't call functions then it probably won't cope with arrays, record types, tsearch, postgis, ... Commented Jul 31, 2012 at 20:15
  • Dapper was written by the folks @ SO... Commented Jul 31, 2012 at 20:23
  • Honestly it hasn't really affected me too much since I can manually alphabetize, it's just annoying. Commented Jul 31, 2012 at 20:31

1 Answer 1

1

Internally, Dapper uses reflection to get the properties of the param object. Specifically, GetProperies(...) trouble is, those aren't guaranteed to be in a particular order...

The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which properties are returned, because that order varies.

In order be somewhat useful, they chose to order the parameters alphabetically, but unfortunately there is no way to ensure that the parameters appear in the order you have them in the class.

Sign up to request clarification or add additional context in comments.

2 Comments

That's not the syntax for named parameters in PostgreSQL, which is what the question is specifically about. I'm not sure how you might coerce Dapper to use the named parameter convention, but the syntax on the PostgreSQL side is described here: postgresql.org/docs/9.1/interactive/…
@kgrittn, you are correct. I did a quick search on google for the postgresql syntax, and found a page on calling the stored proc, I guess it wasn't postgresql after all. I've removed the code bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.