从MVC中的存储过程中检索数据

时间:2021-11-15 02:07:08

I have following two model classes

我有以下两个模型类

public partial class Items {
    public Items() {
        this.Items_RATINGS = new HashSet<Items_RATINGS>();
    }

    public int ITEMID { get; set; }
    public string ITEMNAME { get; set; }
    public virtual ICollection<Items_RATINGS> Items_RATINGS { get; set; }
}

public partial class Items_RATINGS
{
    public int ItemsID { get; set; }
    public byte ItemsRATING { get; set; }
    public string COMMENTS { get; set; }
    public virtual Items Items { get; set; }
 }

I've also created stored procedure to review average rating

我还创建了存储过程来检查平均评分

CREATE PROCEDURE DBO.GET_ITEM_AVERAGERATING
@ITEMID INT
AS
 BEGIN
   SELECT AVG(OVERALLRATING) AS OVERALLRATING FROM ITEMS_RATINGS WHERE ITEMID = @ITEMID GROUP BY ITEMID
END
GO

In model class, I have added

在模型类中,我添加了

public virtual ICollection<GET_ITEM_AVERAGERATING_Result> GET_ITEM_AVERAGERATING { get; set; }

In the controller calls, I tried adding

在控制器调用中,我尝试添加

return View(db.Items.Include(c => c.GET_ITEM_AVERAGERATING).ToList());

However, I'm not getting the value. Its throwing an error.

但是,我没有得到这个价值。它抛出一个错误。

1 个解决方案

#1


0  

You need to add the stored procedure to your edmx model

您需要将存储过程添加到edmx模型中

Here is how to do it

这是怎么做的

1- Open your edmx file

1-打开edmx文件

2- Next right click anywhere on the Edmx designer and choose Add > Function Import.

2-右键单击Edmx设计器上的任意位置,然后选择“添加”>“函数导入”。

3- This will bring up the wizard for adding Function. Choose the procedure you want to add and choose the return type .

3-这将打开添加功能的向导。选择要添加的过程并选择返回类型。

4- Then you can call your stored procedure as a CLR method from your dbcontext.

4-然后,您可以从dbcontext中将存储过程作为CLR方法调用。

#1


0  

You need to add the stored procedure to your edmx model

您需要将存储过程添加到edmx模型中

Here is how to do it

这是怎么做的

1- Open your edmx file

1-打开edmx文件

2- Next right click anywhere on the Edmx designer and choose Add > Function Import.

2-右键单击Edmx设计器上的任意位置,然后选择“添加”>“函数导入”。

3- This will bring up the wizard for adding Function. Choose the procedure you want to add and choose the return type .

3-这将打开添加功能的向导。选择要添加的过程并选择返回类型。

4- Then you can call your stored procedure as a CLR method from your dbcontext.

4-然后,您可以从dbcontext中将存储过程作为CLR方法调用。