如何在代码使用实体框架的情况下调用存储过程

本文关键字:框架 情况下 调用 存储过程 实体 代码 | 更新日期: 2023-09-27 18:24:05

我试图调用下面的存储过程来显示具有特定产品类型的产品列表。

CREATE PROCEDURE filterListSP
    @productType varchar (25)
AS
BEGIN
    SET NOCOUNT ON;
    SELECT 
        ProductId, Description, Price 
    FROM
        tblProduct 
    WHERE
        ProductType = @productType
END
GO

这是调用SP:的代码

dataGridView1.DataSource = naafiDbEntity.Database.SqlQuery<tblProductType>
    ("filterListSP @productType", cboFilter.SelectedValue).ToList();

然而,当我运行此代码时,我会得到以下错误:

必须声明标量变量"@productType"

有人能告诉我我缺了什么吗?

如何在代码使用实体框架的情况下调用存储过程

在dbcontext中的函数中定义如下。然后通过这种方式打电话例如:dbcontext.SP_Insert(data,value);

            public virtual int SP_Insert(string productType, ObjectParameter retValue)
            {
                var xmlDataParameter = xmlData != null ?
                    new ObjectParameter("productType", xmlData) :
                    new ObjectParameter("productType", typeof(string));
                return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("SP_Insert", xmlDataParameter, retValue);
            }

这里的存储过程是SP_Insert

谢谢大家,我终于解决了。

这就是我将代码修改为的原因

    string result = cboFilter.SelectedValue.ToString();
    dataGridView1.DataSource = naafiDbEntity.filterListSP(result);
相关文章: