如何在EF代码优先方法中调用存储过程?

时间:2017-04-27 04:36:45

标签: c# ef-code-first

我使用实体框架采用代码优先方法。你能提供一些关于如何从我的代码中调用存储过程的建议吗?

存储过程:

create procedure sp_GetCountry
As
Begin
    select * 
    from Country
End

Homectrol.cs

 JanContext db = new JanContext ()

 public JsonResult GetCountry_sp()
 {
     string storedprocedure = "sp_GetCountry";
 }

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

尝试使用EntityFramework Reverse POCO生成器 enter image description here

编辑.tt文件,如下所示

 IncludeStoredProcedures = false;

 IncludeStoredProcedures = true;

这将在DbContext中创建函数,其名称为storedprocedure,你需要调用此函数,这将调用storedprocedure

相关问题