如何使用存储过程的实体框架,并使用它mvc4应用 [英] how to use stored procedures in entity framework and use it mvc4 application
问题描述
我想知道如何使用它在实体框架2输入参数的存储过程,并逐步使用MVC4应用这些步SP和PLZ不共享硬codeD的例子
I want to know how to use stored procedure which has 2 input parameters in entity framework and use those SP in MVC4 application step by step and plz dont share hard coded examples
感谢和放大器;问候
Maark
Thanks&Regards Maark
推荐答案
这可能会帮助你,
调用存储过程使用ExecuteStoreQuery功能
ExecuteStoreQuery应该被用于查询的数据。此方法仅当T有一个默认的构造函数,也是一个属性名相同返回的列名的作品。的T可以是任何通用类或任何数据类型,它可能不是一个EF生成的实体的一部分。
"ExecuteStoreQuery" should be used to query data. This method only works if T has a Default Constructor and also a Property name is the same as the returned column names. "T" can be any generic class or any data type and it might not be a part of an EF generated entity.
下面是使用从存储过程的ExecuteStoreQuery的方法来检索数据的过程。
The following is the procedure to retrieve data using the "ExecuteStoreQuery" method from a Stored Procedure.
方法T可以是任何东西,它可能是一个EF生成的实体,也可以是自定义实体,所以首先我创建一个自定义实体EmployeeDetail。在这里,EmployeeDetail属性名称必须相同存储过程的select语句返回的列。
The method "T" can be anything, it may be an EF Generated entity or it may be a Custom Entity, so first I am creating a Custom Entity "EmployeeDetail". Here the EmployeeDetail properties name must be the same as the returned column of the select statement of the Stored Procedure.
//创建自定义类来保存存储过程的结果。
// Creating Custom class to hold result of Stored Procedure
public class EmployeeDetail
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public string DepartmentName { get; set; }
}
// using Object Context (EF4.0)
var parameters = new SqlParameter[4] ;
parameters[0] = new SqlParameter { ParameterName = "autoIds", Value = autoIds };
parameters[1] = new SqlParameter { ParameterName = "inventoryType", Value = inventoryType };
parameters[2] = new SqlParameter { ParameterName = "orderBy", Value = orderByStr };
parameters[3] = new SqlParameter { ParameterName = "SqlString", Value = SQLString };
using (Entities context = new Entities())
{
IEnumerable<EmployeeDetails> empDetails = context.ExecuteStoreQuery<EmployeeDetails>
("exec GetEmployeeData @autoIds, @inventoryType, @orderBy, @SqlString", parameters).ToList();
}
// using DBContext (EF 4.1 and above)
using (Entities context = new Entities())
{
IEnumerable<EmployeeDetails> empDetails = context. Database.SqlQuery
< EmployeeDetails >("exec GetEmployeeData ", null).ToList();
}
这篇关于如何使用存储过程的实体框架,并使用它mvc4应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!