简体   繁体   English

是否有任何可能的方法来返回通过使用 .net Core MVC 中的 Entity Framework Core 的存储过程生成的结果..?

[英]Is there any possible ways to return the results generated through a stored procedure using Entity Framework Core in .net Core MVC..?

I am developing a .net core application in the database first approach.我正在数据库优先方法中开发 .net 核心应用程序。 Along with that i am using Microsoft SQL Server for my database process.除此之外,我还在为我的数据库进程使用 Microsoft SQL 服务器。 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..?有没有可能的方法来做到这一点..?

EF Core documentation: Querying from Raw SQL Include an entity type in your EF model that matches the stored procedure output columns. EF Core 文档: Querying from Raw SQL在您的 EF model 中包含与存储过程 output 列匹配的实体类型。

EF Core provides the following methods to execute a stored procedure: EF Core 提供了以下方法来执行存储过程:

  1. Using DbSet<TEntity>.FromSql()使用DbSet<TEntity>.FromSql()

    Suppose there have a GetStudents stored procedure.假设有一个GetStudents存储过程。 You can execute SP using FromSql method in EF Core in the same way as above, as shown below.可以和上面一样在EF Core中使用FromSql方法执行SP,如下图。

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

    Supposer this have a CreateStudents stored procedure to insert students.假设这有一个CreateStudents存储过程来插入学生。 Then, you could use the following code to call the SP.然后,您可以使用以下代码调用 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 .更多详细信息,请查看文章: Working with Stored Procedure in Entity Framework Core Besides, you could also use the Raw SQL Queries to execute the stored procedure.此外,您还可以使用原始 SQL 查询来执行存储过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Entity Framework Core 使用存储过程删除带有 bool 返回值的实体 - Entity Framework Core using stored procedure to delete entity with bool return 使用 ASP.NET 核心和实体框架执行存储过程? - Execute stored procedure using ASP.NET Core and Entity Framework? 实体框架核心存储过程 - Entity Framework core stored procedure 如何 map 来自 postresql 中的存储过程的结果集,我使用 Entity Framework Core 返回多个游标 - How to map set of results from stored procedure in postresql where I return multiple cursors using Entity Framework Core 无法将存储过程结果合并到 Entity Framework Core - Unable to incorporate stored procedure results into Entity Framework Core Entity Framework Core中的存储过程“枚举未产生结果”错误 - Stored procedure in Entity Framework Core “enumeration yielded no results” error 执行存储过程并返回List<T> 在实体框架核心 - Execute stored procedure and return List<T> in Entity Framework Core Entity Framework Core 3.1 从存储过程返回值 (int) - Entity Framework Core 3.1 Return value (int) from stored procedure 存储过程仍然与Entity Framework Core返回相同的结果 - Stored procedure still return same result with Entity Framework Core ASP.Net Core - 实体框架 - 调用没有返回数据的存储过程(在 void 方法中) - ASP.Net Core - Entity Framework - Call Stored Procedure With No Return Data (in a void method)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM