我想知道如何使用在实体框架中有2个输入参数的存储过程,并逐步在MVC4应用程序中使用这些SP,并且不要共享硬编码示例
感谢和安培;问候 Maark
答案 0 :(得分:4)
这可能会对你有帮助,
使用ExecuteStoreQuery函数调用存储过程
" ExecuteStoreQuery"应该用于查询数据。此方法仅在T具有默认构造函数且属性名称与返回的列名称相同时才有效。 " T"可以是任何泛型类或任何数据类型,它可能不是EF生成实体的一部分。
以下是使用" ExecuteStoreQuery"检索数据的过程。存储过程中的方法。
方法" T"可以是任何东西,它可能是EF生成的实体或它可能是自定义实体,所以首先我创建一个自定义实体" EmployeeDetail"。这里EmployeeDetail属性名称必须与存储过程的select语句的返回列相同。
//创建自定义类以保存存储过程的结果
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();
}
答案 1 :(得分:2)
我的存储过程将一个参数作为输入:
ALTER procedure [dbo].[uspCopyApplicantForApply]
(
@ApplicationId int
)
as
begin
begin tran
Declare @UserId int
Select @UserId = ApplicantId from PostApplication where Id=@ApplicationId
---Copying Applicant Basic Data
insert into Applicants(UserId,Title,FirstName,LastName,DOB,Phone,Mobile,Languages,MaritalStatus
,MaidenName,Address1,Address2,Address3,Address4,Country,VisaNumber,VisaType
,IssueDate,ExpirtyDate,ValidFrom,VisaCountry,PassportNumber,Img,ApplicationId)
Select UserId,Title,FirstName,LastName,DOB,Phone,Mobile,Languages,MaritalStatus
,MaidenName,Address1,Address2,Address3,Address4,Country,VisaNumber,VisaType
,IssueDate,ExpirtyDate,ValidFrom,VisaCountry,PassportNumber,Img,@ApplicationId
from Applicants where UserId=@UserId and ApplicationId is null
Insert into Availability(StartTime,EndTime,Frequency,RecurrenceFreq,StartsOn,DoesNotEnd,EndsAfter,EndsAfterValue,EndsBy
,EndsByDate,UserId,ApplicationId)
select StartTime,EndTime,Frequency,RecurrenceFreq,StartsOn,DoesNotEnd,EndsAfter,EndsAfterValue,EndsBy
,EndsByDate,UserId,@ApplicationId from Availability where UserId=@UserId and ApplicationId is null
Insert into AppExperience(UserId,DateFrom,Title,DateTo,Employer,[Address],ApplicationId)
select UserId,DateFrom,Title,DateTo,Employer,[Address],@ApplicationId
from AppExperience where UserId=@UserId and ApplicationId is null
Insert into Competency(UserId,Title,StartDate,EndDate,ExpiryDate,Marks,Result,Notes,ApplicationId)
select UserId,Title,StartDate,EndDate,ExpiryDate,Marks,Result,Notes,@ApplicationId
from Competency where UserId=@UserId and ApplicationId is null
Insert into [Resume](UserId,Title,[Path],IsDefault,UploadedAt,ApplicationId)
select UserId,Title,[Path],IsDefault,UploadedAt,@ApplicationId
from [Resume] where UserId=@UserId and ApplicationId is null
commit tran
select 'Done'
end
使用实体框架从C#调用它:
InvovaDbContext db = new InvovaDbContext();
List<string> str = db.Database.SqlQuery<string>("exec uspCopyApplicantForApply {0}", postApplication.Id).ToList();
答案 2 :(得分:0)
它可能对你有帮助
DataSet dsDashboardInfoCompanyAdmin = new DataSet();
#region Get TotalConnections(broken down by devices), TotalContracts, Total Connections(at last week) - for company admin
SqlHelper helper = new SqlHelper();
helper.OpenConnection();
ArrayList sqlParameter = new ArrayList();
sqlParameter.Add(new SqlParameter("@TenantID", TenantID));
sqlParameter.Add(new SqlParameter("@CompanyID", CompanyID));
dsDashboardInfoCompanyAdmin = helper.ExecuteDataSet("proc_DashboardInfoCompanyAdmin", sqlParameter);
#endregion