Dapper stored procedures with Postgresql without alphabetical parameters

c# dapper plpgsql postgresql stored-procedures

Question

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;

Accepted Answer

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.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why