Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get output parameter value of a stored procedure using EF Core FromSql(...) is always null. #9277

Closed
irowbin opened this issue Jul 27, 2017 · 7 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@irowbin
Copy link

irowbin commented Jul 27, 2017

Describe what is not working as expected.

Store procedure output parameter is not working as expected. The output value is always null.
I have an statement like this. I've removed other parameters except this OUTPUT param to pointing out the issue. The records are came but the the value of this OUTPUT parameter is null without the exception. Executing the proc in SSMS works just fine but with this FromSql(...) isn't doing anything for output parameter.

-- other param are working as expected but the OUTPUT is not so i hide them.

@todoCounts VARCHAR(MAX) OUTPUT
var items = _context.Set<T>().FromSql("Sp_Todo @todoCounts OUTPUT", 
 new SqlParameter("@todoCounts", SqlDbType.VarChar)
 { Direction = ParameterDirection.Output}).ToList() // no luck

If i add this too, then the output value coming but this is not the correct idea here. I am not seeing any exceptions.

  _context.Database.ExecuteSqlCommand("Sp_Todo @todoCounts OUTPUT", 
 new SqlParameter("@todoCounts", SqlDbType.VarChar)
 { Direction = ParameterDirection.Output});

If you are seeing an exception, include the full exceptions details (message and stack trace).

none

Steps to reproduce

  • Create a store procedure with output parameter.
 @todoCounts VARCHAR(MAX) OUTPUT
  • Execute the StoreProc like this.
// records retrieved except OUTPUT parameter. 
context.set<MyModel>().FromSql("Sp_Todo @todoCounts OUTPUT", 
 new SqlParameter("@todoCounts", SqlDbType.VarChar)
 { Direction = ParameterDirection.Output}).ToList() 

Further technical details

EF Core version: 1.1.2
Database Provider: 1.1.2
Operating system: Windows 10
IDE: Visual Studio 2017 15.2(2643.16) release

@divega
Copy link
Contributor

divega commented Jul 28, 2017

We need to investigate if this is working on 2.0. We will consider patching this depending on the impact and the fix.

@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 31, 2017
@smitpatel smitpatel removed this from the 2.1.0 milestone Jul 31, 2017
@smitpatel
Copy link
Member

Since fix for this is out in PR, removing milestone so we can discuss about patch.

@smitpatel smitpatel removed the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 31, 2017
@smitpatel smitpatel added this to the 2.1.0 milestone Jul 31, 2017
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 31, 2017
@mguinness
Copy link

mguinness commented Aug 24, 2017

Just to clarify that this issue is in relation to SQL Server. The MySQL database provider has the same issue, but there is a known problem which prevents output parameters from being used with Connector/NET.

dougbu added a commit to dougbu/EntityFramework_7048 that referenced this issue Oct 18, 2017
dougbu added a commit to dougbu/EntityFramework_7048 that referenced this issue Oct 18, 2017
@CharlesOkwuagwu
Copy link

Please is there a work-around for this in the interim?

@ajcvickers
Copy link
Member

@CharlesOkwuagwu Probably the best workaround is to get the DbConnection using context.Database.GetDbConnection() and then create the command and execute it directly using DbConnection/DbCommand, etc.

@CharlesOkwuagwu
Copy link

@ajcvickers Thanks, will try that option.

@mhsadiqhussain
Copy link

Please use the below code, It works:

SqlParameter spOutVar;

var result = context.set().FromSql("Sp_Todo @todoCounts OUTPUT",
spOutVar = new SqlParameter("@todoCounts", SqlDbType.VarChar) { Direction = ParameterDirection.Output}).ToList();

int outputCount = int.Parse(spOutVar.Value.ToString());

Happy Coding :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

7 participants