Using DbParameter w/ ExecuteSqlCommand in EF Core

entity-framework-core

Question

Unfortunately there doesn't appear to be any documentation for ExecuteSqlCommand, I could only find documentation for FromSql. I'm able to successfully call a stored procedure using ExecuteSqlCommand with named parameters that returns rows affected.

However, I'm attempting to change the Direction property of one DbParameter to ParameterDirection.Output. This doesn't appear to work and I can find no examples anyway of this usage. The closest I saw was issue #3115 in the EF Core GitHub repo, but there was no usage of out parameters.

Of course I can use DbCommand to achieve this, but does ExecuteSqlCommand only use DbParameter for named parameters and direction is ignored? If so, then ideally this should be documented somewhere - maybe the Raw SQL Queries article can be expanded upon.

1
1
4/4/2017 4:52:50 PM

Accepted Answer

This is a database provider specific issue since it doesn't work for MySQL, but works for SQL Server. Below is code that demonstrates use for the latter:

var outParam = new SqlParameter("@ParamOut", DbType.Int32)
    { Direction = ParameterDirection.Output };

ctx.Database.ExecuteSqlCommand("EXEC dbo.MyTest @ParamOut OUTPUT", outParam);
1
4/8/2017 2:49:03 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow