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

前端 未结 21 2517
滥情空心
滥情空心 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:57

    It work for me at code first. It return a list with matching property of view model(StudentChapterCompletionViewModel)

    var studentIdParameter = new SqlParameter
    {
         ParameterName = "studentId",
         Direction = ParameterDirection.Input,
         SqlDbType = SqlDbType.BigInt,
         Value = studentId
     };
    
     var results = Context.Database.SqlQuery(
                    "exec dbo.sp_StudentComplettion @studentId",
                     studentIdParameter
                    ).ToList();
    

    Updated for Context

    Context is the instance of the class that Inherit DbContext like below.

    public class ApplicationDbContext : DbContext
    {
        public DbSet City { get; set; }
    }
    
    var Context = new  ApplicationDbContext();
    

提交回复
热议问题