[英]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 提供了以下方法来执行存储过程:
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();
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.