Click here to Skip to main content
15,885,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.
I am trying to understand this method for a whole day long now now. I need to call a stored procedure, which does not return, or at least does not suppose to return anything. Just like a void method call. However, no matter what I am trying to do, it seems like such a pathetically childish simple task is not simply possible - is it correct? If it is - it is mind blowing.

What I have tried:

Simple SP:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[procedure_name]
AS
select 1
GO


Now I am trying to call this:
context.MyClassSet.FromSqlRaw("EXEC procedure_name;")

Which is of course
DbSet<MyClass> MyClassSet;


MyClass looks like this:
public class MyClass
{
    // public int id { get; set; }
}


The error in this case is: sequence contains no elements. If I uncomment id property - the error is: The required id bla bla is not present something.
The only thing I want, is just to call stored procedure and return nothing.
Can anyone help please?
Posted
Updated 2-Jun-21 22:39pm
v3

If you don't want to return any results, don't use FromSqlRaw on a DbSet; use the ExecuteSql* extension methods on the Database façade.
C#
int returnValue = context.Database.ExecuteSqlRaw("EXEC procedure_name;");
RelationalDatabaseFacadeExtensions.ExecuteSqlRaw Method (Microsoft.EntityFrameworkCore) | Microsoft Docs[^]
 
Share this answer
 
Comments
csrss 4-Jun-21 9:20am    
Thanks
Your query has nothing to do with EF; there is no relation between it and any EF entities; it's a simple C# SQL SP call. (If MyClassSet had elements, it might work, but that's only because you made your query dependent on it)

sql server - How to execute a stored procedure within C# program - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900