简体   繁体   English

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

[英]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 : 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 尝试使用EntityFramework Reverse POCO生成器 在此输入图像描述

edit the .tt file as below 编辑.tt文件如下

 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 这将在DbContext中创建函数,其名称为storedprocedure你需要调用此函数,这将调用storedprocedure

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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