2

I have a stored procedure like this:

CREATE PROCEDURE InsertTest(@p1 NVARCHAR(50) , @p2 INT)
AS 
BEGIN 
   INSERT INTO dbo.Test(Name, Code)
   VALUES ( N'', 0)
END

or other

I try with this code:

context.Database.SqlQuery<int>("InsertTest @p1 , @p2", "a",1);

But this leads to error.

How can I run it using DbContext ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ramin_rp
  • 321
  • 1
  • 4
  • 14
  • what error? shouldn't you call `ExecuteSqlCommand`? check [here](http://stackoverflow.com/questions/6219643/how-to-call-stored-procedure-in-mvc-by-ef) and [here](http://stackoverflow.com/questions/14264750/how-to-call-stored-procedures-with-entityframework) – Yuliam Chandra Jul 21 '14 at 13:04
  • Possible duplicate of [Calling a stored procedure with nothing to return using Entity Framework](https://stackoverflow.com/questions/16990666/calling-a-stored-procedure-with-nothing-to-return-using-entity-framework) – freedomn-m Feb 20 '18 at 10:22

1 Answers1

3

Do this.

context.Database.ExecuteSqlCommand(
   "InsertTest @p1 , @p2", 
   new SqlParameter("@p1", "a"),
   new SqlParameter("@p2", 1));

or

context.Database.SqlQuery<object>(
   "exec InsertTest @p1 , @p2", 
   new SqlParameter("@p1", "a"),
   new SqlParameter("@p2", 1)).FirstOrDefault();
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67