Call stored procedure via DBContext

Tried to call a SP via Entity framework (DBContext) but got a exception that parameter is not supplied.

My call looked like:
context.Database.ExecuteSqlCommand(“NameOfSP”, new SqlParameter(“@param1”, 1), new SqlParameter(“@param2”, DateTime.Now), new SqlParameter(“@param3”, “test”));

The right call should look like:
context.Database.ExecuteSqlCommand(“NameOfSP @p1, @p2, @p3”, new SqlParameter(“@p1”, 1), new SqlParameter(“@p2”, DateTime.Now), new SqlParameter(“@p3”, “test”));

You should include the parameter name after the name of the stored procedure.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.