0

I am developing a .net core application in the database first approach. Along with that i am using Microsoft SQL Server for my database process. I am trying to return the results generated in the stored procedure to the application using entity framework core but, i'm unable to find a way. Is there any possible ways to do it..?

Sanjay
  • 3
  • 1
  • Does this answer your question? [How to run stored procedures in Entity Framework Core?](https://stackoverflow.com/questions/28599404/how-to-run-stored-procedures-in-entity-framework-core) – hujtomi Aug 10 '20 at 20:35

2 Answers2

0

EF Core documentation: Querying from Raw SQL Include an entity type in your EF model that matches the stored procedure output columns.

pjs
  • 363
  • 1
  • 7
0

EF Core provides the following methods to execute a stored procedure:

  1. Using DbSet<TEntity>.FromSql()

    Suppose there have a GetStudents stored procedure. You can execute SP using FromSql method in EF Core in the same way as above, as shown below.

     var context = new SchoolContext(); 
    
     var students = context.Students.FromSql("GetStudents 'Bill'").ToList();
    
  2. Using DbContext.Database.ExecuteSqlCommand()

    Supposer this have a CreateStudents stored procedure to insert students. Then, you could use the following code to call the SP.

    var context = new SchoolContext(); 
    
    context.Database.ExecuteSqlCommand("CreateStudents @p0, @p1", parameters: new[] { "Bill", "Gates" });
    

More detail information, please check the article:Working with Stored Procedure in Entity Framework Core. Besides, you could also use the Raw SQL Queries to execute the stored procedure.

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30