question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't execute reverse engineered Stored procedures

See original GitHub issue

This is a regression introduced by #1069

Steps to reproduce

Create a database and get the reverse engineered model

CREATE DATABASE [ConUniv]
GO
USE [ConUniv]
GO
CREATE TABLE [dbo].[Course](
	[Id] [uniqueidentifier] NOT NULL,
	[Title] [varchar](50) NOT NULL,
	[Credits] [int] NOT NULL,
	[DepartmentID] [uniqueidentifier] NOT NULL,
	[Budget] [money] NOT NULL,
 CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE PROCEDURE [dbo].[spCourseCount]	
AS
BEGIN
	SET NOCOUNT ON;
	Select Count(*) CourseCount From Course
END
GO

When I try to execute an SP I get the following error

Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): The parameterized query '(@returnValue int output)EXEC @returnValue = [dbo].[spCourseCoun' expects the parameter '@returnValue', which was not supplied.
   at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__169_0(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__277_0(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at EFTools.Models.DbContextExtensions.SqlQueryAsync[T](DbContext db, String sql, Object[] parameters, CancellationToken cancellationToken) in C:\source\repos\EFTools\EFTools\Models\DbContextExtensions.cs:line 28
   at EFTools.Models.ConunivContextProcedures.spCourseCountAsync(OutputParameter`1 returnValue, CancellationToken cancellationToken) in C:\source\repos\EFTools\EFTools\Models\ConunivContextProcedures.cs:line 58
   at EFTools.Program.Main(String[] args) in C:\source\repos\EFTools\EFTools\Program.cs:line 12
   at EFTools.Program.<Main>(String[] args)
ClientConnectionId:e212810a-f4e2-4eb2-bb32-ce71b81c9836
Error Number:8178,State:1,Class:16
class Program
    {
        static async Task Main(string[] args)
        {
            var context = new ConunivContext();
            var result = await context.Procedures.spCourseCountAsync();
            Console.WriteLine(result.Single().CourseCount);
        }
    }

It works fine if I change manually the Direction to System.Data.ParameterDirection.Output in spCourseCountAsync

public virtual async Task<List<spCourseCountResult>> spCourseCountAsync(OutputParameter<int> returnValue = null, CancellationToken cancellationToken = default)
        {
            var parameterreturnValue = new SqlParameter
            {
                ParameterName = "returnValue",
                Direction = System.Data.ParameterDirection.Output, // InputOutput
                SqlDbType = System.Data.SqlDbType.Int,
            };

            var sqlParameters = new []
            {
                parameterreturnValue,
            };
            var _ = await _context.SqlQueryAsync<spCourseCountResult>("EXEC @returnValue = [dbo].[spCourseCount]", sqlParameters, cancellationToken);

            returnValue?.SetValue(parameterreturnValue.Value);

            return _;
        }

Further technical details

EF Core Power Tools version: 2.5.731

Database engine: SQL Server

Visual Studio version: Visual Studio 2019 16.11.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ErikEJcommented, Oct 19, 2021

I am working on a better fix now! 😀

0reactions
whschultzcommented, Oct 19, 2021

Migrated discussion back to the other issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reverse engineer stored procedure no generate EntityResult
Hi, I have created the netx Stored Procedure: `CREATE PROCEDURE dbo.TestPA @informeId INT, @checkresult BIT AS BEGIN SET NOCOUNT ON; ...
Read more >
Reverse Engineer sp_cursorexecute
When I trace the session all I am getting back is a statement calling sp_cursorexecute which references an ID number. I am assuming...
Read more >
Cannot create stored procedures with EXECUTE AS using ...
Cannot execute as the database principal because the principal "TestReportUser" does not exist, this type of principal cannot be impersonated, ...
Read more >
Reverse Engineer Processing Errors
When you reverse engineer a script or a database, parser errors may occur. Parser errors often occur when there is a mismatch between...
Read more >
Reverse Mapping Oracle Stored Procedures
My second issue is that this is not reverse engineered from the database correctly so how can I update it in the model...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found