如何通过entityframework从webapi返回storedproc数据 [英] how to return storedproc data from webapi through entityframework

查看:91
本文介绍了如何通过entityframework从webapi返回storedproc数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这是第一次在我的项目中获得.edmx文件并导入函数getallcompanies我没有得到如何在webapi中调用storedproc。任何人请帮助我从早上开始尝试。这是我的webapi控制器



公共类CompanyServiceController:ApiController

{

私有PTMEntities db =新PTMEntities( );

public IEnumerable< Company> GetAllCompanies()

{



使用(var db = new PTMEntities())

{



返回db.GetAllCompanies();

}



}



i完全与界面存储库混淆在一些网站中存在而在某些网站中没有。



在model1.contect中.cs我有这个错误



public virtual ObjectResult< GetAllCompanies_Result> GetAllCompanies()

{

return((IObjectContextAdapter)this).ObjectContext.ExecuteFunction< GetAllCompanies_Result>(" GetAllCompanies");

}

}



我得到的是Model.tt



GetAllCompanies_Result .cs

I am doing this first time I got .edmx file in my project and imported function getallcompanies i am not getting how to call a storedproc in webapi . can any one please help me am trying this from morning . this is my webapi controller

public class CompanyServiceController : ApiController
{
private PTMEntities db = new PTMEntities();
public IEnumerable<Company> GetAllCompanies()
{

using (var db = new PTMEntities())
{

return db.GetAllCompanies();
}

}

i am totally confused with Interface repository is there in some sites and not there in somesites.

And in model1.contect.cs i have this with error

public virtual ObjectResult<GetAllCompanies_Result> GetAllCompanies()
{
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetAllCompanies_Result>("GetAllCompanies");
}
}

and i got Model.tt which has

GetAllCompanies_Result.cs

推荐答案

你好



在edmx表(实体)中,sp有自己的cs文件。

所以,如果你有ed in edmx并且你在Model.tt上得到它,那么你就像这样使用: -





Hello

in edmx table(entitys) and sp have there own cs files.
SO when you have sp in edmx and you get it on Model.tt then you used like this:-


public IEnumerable<GetAllCompanies_Result> GetAllCompanies()
{
 
using (var db = new PTMEntities())
{
 
return db.GetAllCompanies();
}
 
}





这里你返回的类型是



here you return type is

GetAllCompanies_Result



不是公司,这就是为什么你不回来并得到错误。

我希望你的问题能解决


not company thats why you not return and get error.
I hope you problem will resolve


这篇关于如何通过entityframework从webapi返回storedproc数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆