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

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

    When EDMX create this time if you select stored procedured in table select option then just call store procedured using procedured name...

    var num1 = 1; 
    var num2 = 2; 
    
    var result = context.proc_name(num1,num2).tolist();// list or single you get here.. using same thing you can call insert,update or delete procedured.
    
    0 讨论(0)
  • 2020-11-22 05:57

    This works for me by pulling back data from a stored procedure while passing in a parameter.

    var param = new SqlParameter("@datetime", combinedTime);
    var result = 
            _db.Database.SqlQuery<QAList>("dbo.GetQAListByDateTime @datetime", param).ToList();
    

    _db is the dbContext

    0 讨论(0)
  • 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<StudentChapterCompletionViewModel>(
                    "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> City { get; set; }
    }
    
    var Context = new  ApplicationDbContext();
    
    0 讨论(0)
  • 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<ABCDbContext> options)
           : base(options)
        {
    
        }
    
     public DbSet<SP_reslutclass> SP_reslutclass { get; set; }
    }
    

    Call entity in repository

       var counts = _Dbcontext.SP_reslutclass.FromSql("call SP_Dasboarddata 
                        ('2019-12-03','2019-12-31')").ToList();
    
    0 讨论(0)
  • 2020-11-22 05:59

    Using MySql and Entity framework code first Approach:

    public class Vw_EMIcount
    {
        public int EmiCount { get; set; }
        public string Satus { get; set; }
    }
    
    var result = context.Database.SqlQuery<Vw_EMIcount>("call EMIStatus('2018-3-01' ,'2019-05-30')").ToList();
    
    0 讨论(0)
  • 2020-11-22 06:03

    Take a look to this link that shows how works the mapping of EF 6 with Stored Procedures to make an Insert, Update and Delete: http://msdn.microsoft.com/en-us/data/dn468673

    Addition

    Here is a great example to call a stored procedure from Code First:

    Lets say you have to execute an Stored Procedure with a single parameter, and that Stored Procedure returns a set of data that match with the Entity States, so we will have this:

    var countryIso = "AR"; //Argentina
    
    var statesFromArgentina = context.Countries.SqlQuery(
                                          "dbo.GetStatesFromCountry @p0", countryIso
                                                        );
    

    Now lets say that we whant to execute another stored procedure with two parameters:

    var countryIso = "AR"; //Argentina
    var stateIso = "RN"; //Río Negro
    
    var citiesFromRioNegro = context.States.SqlQuery(
                                "dbo.GetCitiesFromState @p0, @p1", countryIso, stateIso
                              );
    

    Notice that we are using index-based naming for parameters. This is because Entity Framework will wrap these parameters up as DbParameter objects fro you to avoid any SQL injection issues.

    Hope this example helps!

    0 讨论(0)
提交回复
热议问题