我正在使用具有存储库模式的实体框架来尝试访问存储过程
通用存储库模式
public virtual IEnumerable<T> ExecuteStoreProcFunction(string query, params object[] parameters)
{
return _entities.Database.SqlQuery<T>(query, parameters).ToList();
}
使用以下命令
var reportFilterDb =
_iGenericReportProcRepository.ExecuteStoreProcFunction("StoreProcReport @QuotationNo, @AgencyName, @ContractStartDate, @ContractEndDate, @contractTerm",
new SqlParameter("QuotationNo", SqlDbType.VarChar) { Value = filter.QuotationNo },
new SqlParameter("AgencyName", SqlDbType.VarChar) { Value = filter.AgencyName },
new SqlParameter("ContractStartDate", SqlDbType.DateTime) { Value = filter.FContractStartDate },
new SqlParameter("ContractEndDate", SqlDbType.DateTime) { Value = filter.FContractEndDate },
new SqlParameter("contractTerm", SqlDbType.Int) { Value = filter.Term }
).ToList();
这是商店程序结构
CREATE PROCEDURE [dbo].[StoreProcReport]
@QuotationNo varchar(max) null,
@AgencyName varchar(max) null,
@ContractStartDate datetime null,
@ContractEndDate datetime null,
@contractTerm int null
AS
SET NOCOUNT ON;
SELECT con.Contract_AgencyName, con.Contract_EndDate, con.Contract_Id as 'ContractId', con.Contract_QuoteNo, con.Contract_StartDate,
ConSer.ContractService_Id, ConSer.Description,ConSer.MonthlyUnitPrice, ConSer.OrderEndDate, ConSer.OrderStartDate,ConSer.OrderTermMonths,
ConSer.Quantity, ConSer.TotalPrice
FROM Contract AS con join ContractService as ConSer on con.Contract_Id = ConSer.Contract_Id
WHERE (con.Contract_QuoteNo = @QuotationNo OR @QuotationNo IS NULL)
AND (con.Contract_AgencyName = @AgencyName OR @AgencyName IS NULL)
AND (con.Contract_StartDate = @ContractStartDate OR @ContractStartDate IS NULL)
AND (con.Contract_EndDate = @ContractEndDate OR @ContractEndDate IS NULL)
AND (ConSer.OrderTermMonths = @contractTerm OR @contractTerm IS NULL)
GO
我,我无法执行错误的结果
参数化查询&#39;(@ QuotationNo varchar(),@ AgencyName varchar(),@ ContractS&#39;需要参数&#39; @QuotationNo&#39;,这是未提供的
答案 0 :(得分:4)
如果filter.QuotationNo
为null
,则不会添加参数 ,这是一种糟糕的API体验。请改为:.Value = (object)filter.QutationNo ?? DBNull.Value
(所有参数等)。或者,考虑像Dapper这样的工具,它会自动执行此类操作:
_connection.Execute("StoreProcReport", new { // or Query<T> etc
filter.QuotationNo,
filter.AgencyName,
ContractStartDate = filter.FContractStartDate,
ContractEndDate = filter.FContractEndDate,
contractTerm = filter.Term,
}, commandType: CommandType.StoredProcedure);
答案 1 :(得分:0)
这项工作对我来说
var reportFilterDb =
_iGenericReportProcRepository.ExecuteStoreProcFunction("EXEC StoreProcReport @QuotationNo, @AgencyName, @ContractStartDate, @ContractEndDate, @contractTerm",
new SqlParameter("@QuotationNo", SqlDbType.VarChar) { Value = (object)filter.QuotationNo ?? DBNull.Value },
new SqlParameter("@AgencyName", SqlDbType.VarChar) { Value = (object)filter.AgencyName ?? DBNull.Value },
new SqlParameter("@ContractStartDate", SqlDbType.DateTime) { Value = (object)filter.FContractStartDate ?? DBNull.Value },
new SqlParameter("@ContractEndDate", SqlDbType.DateTime) { Value = (object)filter.FContractEndDate ?? DBNull.Value },
new SqlParameter("@contractTerm", SqlDbType.Int) { Value = (object)filter.Term ?? DBNull.Value }
).ToList();