I'm having an issue with the ExecuteSqlCommand() method in Entity Framework (EF). I have the following SQL Server stored procedure:
EXEC SomeSchema.SomeTableInsert
@Id = 1,
@SomeData = 'This is some random data',
@UserId = 500
and it works without any issues. When I try to use EF's ExecuteSqlCommand() method, I get the following error:
Procedure or function 'SomeTableInsert' expects parameter '@UserId', which was not supplied.
Here is the C# code:
var sql = "SomeSchema.SomeTableInsert @Id, @SomeData, @UserId";
var sqlParameters = new List<SqlParameter>
{
new SqlParameter("@Id", 1),
new SqlParameter("@SomeData", "This is some random data"),
new SqlParameter("@UserId", 500)
};
dbContext.Database.ExecuteSqlCommand(sql, sqlParameters.ToArray<object>());
I don't understand why I'm getting this error. The UserId parameter is not missing and it has a valid value. Any ideas?
Update:
Here is the SQL that is run on SQL Server:
exec sp_executesql
N'SomeSchema.SomeTableInsert @Id, @SomeData, @UserId',
N'@Id int,@SomeData nvarchar(5),@UserId int',
@Id=1,
@SomeData='This is some random data',
@UserId=500
ExecuteSqlCommand(sql, new SqlParameter("@Id", 1), new SqlParameter("@SomeData", "This is some random data"), new SqlParameter("@UserId", 500);?