![](/img/trans.png)
[英]How to retrieve a value returned from a stored procedure using entity Framework?
[英]Retrieve table data from stored procedure using entity framework
我正在使用Entity Framework v6。 我有一個存儲過程,如下所示
CREATE PROCEDURE [dbo].[GetCountryList]
(
@CustomerName VARCHAR(MAX),
@SearchCriteria VARCHAR(MAX)
)
AS
BEGIN
SET NOCOUNT ON
SELECT CountryID, CountryName FROM dbo.Table1
WHERE CustomerName = @CustomerName AND CountryName = @SearchCriteria
END
現在我有一個模型類
public class CountryName
{
public int CountryId { get; set; }
public string CountryName { get; set; }
}
所以我想在List<CountryName>
類型中獲得SELECT
查詢的結果
List<CountryName> countryList = null;
using (DbEntities dbContext = new DbEntities())
{
countryList = //my code to collect the result
}
好吧,我可以直接在表上運行LINQ to SQL,但不幸的是我要求從存儲過程中獲取數據。 那么,我該怎么做呢?
Add -> Function Import
。 GetCountryListSP
,從下拉列表中選擇您的過程,然后選擇過程的返回值為Entities
並從中選擇CountryName
下拉列表。 然后在代碼中:
var result = db.GetCountryListSP();//Send parameters too
使用此方法可以防止返回存儲過程的-1
。 有關Entity Framework存儲過程問題的更多詳細信息,請查看此帖子。
你可以在不導入的情況下完成。 像這樣的東西:
var countryList = dbContext.Database.SqlQuery<CountryName>("[GetCountryList]").ToList();
EntityFramework有時無法識別或導入SP)))所以,這就是我用這個片段節省我的時間的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.