简体   繁体   中英

How to call stored procedure in EF code-first approach?

I'm using Entity Framework with a code-first approach. Can you provide some suggestions on how to call a stored procedure from my code?

Stored procedure:

create procedure sp_GetCountry
As
Begin
    select * 
    from Country
End

Homectrol.cs :

 JanContext db = new JanContext ()

 public JsonResult GetCountry_sp()
 {
     string storedprocedure = "sp_GetCountry";
 }
public JsonResult GetCountry_sp()
{
    // string storedProcedure = "sp_GetCountry";
    var Country = db.Countries.SqlQuery("sp_GetCountry").ToList();
    return new JsonResult { Data = Country, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

Try to use of EntityFramework Reverse POCO generator 在此输入图像描述

edit the .tt file as below

 IncludeStoredProcedures = false;

to

 IncludeStoredProcedures = true;

This will create function in the DbContext with the name of storedprocedure u need to call this function this will call to the storedprocedure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM