Calling a simple stored procedure in EF core 3.1

前端 未结 1 977
轮回少年
轮回少年 2020-12-04 03:36

I have a very simple stored procedure:

CREATE PROCEDURE [dbo].[ClearIterations]
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    --         


        
相关标签:
1条回答
  • 2020-12-04 04:16

    EF Core 3.x+ provides two raw SQL sets of methods - FromSql and ExecuteSql, both with Raw / Interpolated and Async versions.

    The former are used for querying. They return IQueryable<T>, allow query composition and as any LINQ query are not executed until the result is enumerated.

    While the later is used to immediately execute arbitrary SQL (DDL, DML, batch etc.). They are EF Core equivalent of ADO.NET ExecuteNonQuery and return the records affected. Output (or input/output) primitive value parameters can be used to obtain the results.

    Shortly, ExecuteSql methods are what you are seeking for. With your example, ExecuteSqlRaw, e.g. (assuming this is method in your DbContext derived class):

    public void ClearIterations()
    {
        this.Database.ExecuteSqlRaw("ClearIterations");
    }    
    
    0 讨论(0)
提交回复
热议问题