Always getting the first row of the table when using dapper

dapper

Question

When I am using dapper I am keep getting the first row of the table eventhough I am trying to Pass specific ID but i am still getting the first row from the table. What is it I am missing? I checked in the sql profiler and found that id is not being sent to the stored procedure and all the data is returend. I tired many things but dont know why id is not being passed.

public Setting GetConfigurationPerIdy(string id)
{
    return Run(conn => conn.Query<Setting>("spProc_GetSettingById",
        new {id}).FirstOrDefault());
}

Expert Answer

As with regular ADO.NET, the default assumption is CommandType.CommandText; if the command you are executing is a stored procedure, you need to tell it:

..., new {id}, commandType: CommandType.StoredProcedure

Otherwise, it is functionally identical to:

declare @id int = 7; -- or whatever
exec spProc_GetSettingById; -- note that we didn't add the parameter

Popular Answer

Remove FirstOrDefault() from your code, As it will return only first row from the table else default null.




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