How to call Stored Procedure in Entity Framework 6 (Code-First)?

前端 未结 21 2520
滥情空心
滥情空心 2020-11-22 05:04

I am very new to Entity Framework 6 and I want to implement stored procedures in my project. I have a stored procedure as follows:

ALTER PROCEDURE [dbo].[ins         


        
21条回答
  •  情书的邮戳
    2020-11-22 05:44

    I found that calling of Stored Procedures in Code First approach is not convenient. I prefer to use Dapper instead

    The following code was written with Entity Framework:

    var clientIdParameter = new SqlParameter("@ClientId", 4);
    
    var result = context.Database
    .SqlQuery("GetResultsForCampaign @ClientId", clientIdParameter)
    .ToList();
    

    The following code was written with Dapper:

    return Database.Connection.Query(
                "GetResultsForCampaign ",
                new
                {
                    ClientId = 4
                },
                commandType: CommandType.StoredProcedure);
    

    I believe the second piece of code is simpler to understand.

提交回复
热议问题