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

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

    Create Procedure in MYsql.

    delimiter //
    create procedure SP_Dasboarddata(fromdate date, todate date)
    begin
    select count(Id) as count,date,status,sum(amount) as amount from 
    details
    where (Emidate between fromdate and todate)
    group by date ,status;
    END;
    //
    

    Create class which contains stored procedure return result set values

    [Table("SP_reslutclass")]
    public  class SP_reslutclass
    {
        [Key]
        public int emicount { get; set; }
        public DateTime Emidate { get; set; }
        public int ? Emistatus { get; set; }
        public int emiamount { get; set; }
    
    }
    

    Add Class in Dbcontext

      public  class ABCDbContext:DbContext
    {
        public ABCDbContext(DbContextOptions options)
           : base(options)
        {
    
        }
    
     public DbSet SP_reslutclass { get; set; }
    }
    

    Call entity in repository

       var counts = _Dbcontext.SP_reslutclass.FromSql("call SP_Dasboarddata 
                        ('2019-12-03','2019-12-31')").ToList();
    

提交回复
热议问题