首先使用MVC 5和Entity Framework 6中的存储过程

时间:2021-08-24 02:16:09

I am working on my first ASP.NET MVC 5 application and I'm not sure how to call my stored procedures. When I started the project, I was using ADO.NET Entity Data Model, EF Designer, when creating the model I chose the tables and the stored procedures I wanted and it worked fine. For example, if I wanted to use a stored procedure called 'usp_RhAffSelect' I would simply do:

我正在研究我的第一个ASP.NET MVC 5应用程序,我不知道如何调用我的存储过程。当我启动项目时,我正在使用ADO.NET实体数据模型,EF Designer,在创建模型时,我选择了表格和我想要的存储过程,并且工作正常。例如,如果我想使用名为'usp_RhAffSelect'的存储过程,我只会这样做:

using (EmployeModele db = new EmployeModele())
        {var AffectationEmploye = db.usp_RhAffSelect(employeID, sequence).ToList();
...

and the result from the select query would be stored in 'AffectationEmploye'

并且select查询的结果将存储在'AffectationEmploye'中

But then I had to change my model to code first instead of EF Designer. I selected the tables I wanted (which are RhAff,RhEmp and RhEve). So I have my MainModel.cs (which is my DbContext) that describes all my tables fields inside the OnModelCreating method. I also have 3 partial classes (RhAff.cs, RhEmp.cs and RhEve.cs) that also describes their respective fields.

但后来我不得不将我的模型改为代码而不是EF Designer。我选择了我想要的表格(RhAff,RhEmp和RhEve)。所以我有我的MainModel.cs(这是我的DbContext),它描述了OnModelCreating方法中的所有表字段。我还有3个部分类(RhAff.cs,RhEmp.cs和RhEve.cs),它们也描述了它们各自的字段。

Now I don't know how to use my stored procedures. It seems that I can use the MapToStoredProcedures but from my understanding I can only redefine insert, update and delete queries.

现在我不知道如何使用我的存储过程。似乎我可以使用MapToStoredProcedures但从我的理解我只能重新定义插入,更新和删除查询。

How can I call a select stored procedure for my models? Or any stored procedure in particular? When I was using EF Designer, I would simply right click on a table in my edmx file and would select 'Update from database', I would choose which stored procedure I wanted and was able to use them after.

如何为模型调用select存储过程?或者特别是任何存储过程?当我使用EF Designer时,我只需右键单击我的edmx文件中的一个表并选择“从数据库更新”,我会选择我想要的存储过程,然后才能使用它们。

1 个解决方案

#1


0  

You can do that as shown below.

你可以这样做,如下所示。

Note: This is just an example.

注意:这只是一个例子。

SP

CREATE procedure [dbo].[CountOfOrders]
    @ProductId int
AS
SELECT Count(*) From Orders o 
INNER JOIN OrderDetails od ON od.OrderID = o.OrderID 
WHERE od.ProductID = @ProductId

How to call it :

怎么称呼它:

SqlParameter param1 = new SqlParameter("@ProductID", 72); 
var totalOrders = await context.Database.SqlQuery<int>("CountOfOrders @ProductID", param1).SingleAsync();

Please read Entity Framework Code First and Stored Procedures article.

请阅读实体框架代码优先和存储过程文章。

#1


0  

You can do that as shown below.

你可以这样做,如下所示。

Note: This is just an example.

注意:这只是一个例子。

SP

CREATE procedure [dbo].[CountOfOrders]
    @ProductId int
AS
SELECT Count(*) From Orders o 
INNER JOIN OrderDetails od ON od.OrderID = o.OrderID 
WHERE od.ProductID = @ProductId

How to call it :

怎么称呼它:

SqlParameter param1 = new SqlParameter("@ProductID", 72); 
var totalOrders = await context.Database.SqlQuery<int>("CountOfOrders @ProductID", param1).SingleAsync();

Please read Entity Framework Code First and Stored Procedures article.

请阅读实体框架代码优先和存储过程文章。