如何首先使用代码导入存储过程 ASP.NET MVC 和 Entity Framework
How to import stored procedure using code first ASP.NET MVC and Entity Framework
我想先使用 EF 代码导入存储过程,该代码是从 SQL 服务器创建的。我试图检查发布的文章,但到目前为止我似乎没有找到我理解的东西。下面的 SQL 语句是 3 个表的连接,所以如果我可以使用下面的示例代码获得如何解决它的示例。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InvoiceReport]
AS
SELECT
Ordering.order_id,
Ordering.CustomerId,
Ordering.invoice_number,
Ordering.date_order_placed,
Customer.FirstName,
Customer.LastName,
Customer.EmailId,
Invoice_Line_Items.item,
Invoice_Line_Items.department,
Invoice_Line_Items.service,
Invoice_Line_Items.gender,
Invoice_Line_Items.quantity,
Invoice_Line_Items.price,
Invoice_Line_Items.pick_up_address,
Invoice_Line_Items.pick_up_date,
Invoice_Line_Items.pick_up_time,
Invoice_Line_Items.drop_off_address,
Invoice_Line_Items.drop_off_date,
Invoice_Line_Items.drop_off_time
FROM
Ordering
JOIN
Customer ON Ordering.CustId = Customer.CustId
JOIN
Invoice_Line_Items ON Customer.CustId = Invoice_Line_Items.CustId
RETURN 0
我平时使用存储过程的方式如下:
- 添加 EF 代码第一次迁移(https://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx)
- 通过
Sql()
将存储过程添加到您的迁移中
- 使用模型构建器或在您的实体中配置存储过程
- 在您的代码中使用它:-)
迁移示例:
public partial class yourmigration: DbMigration
{
protected override void Up()
{
Sql("CREATE PROCEDURE SQL HERE");
}
//dont forget the down.
}
对于 EF 6,这里有一篇关于它的好文章:https://www.entityframeworktutorial.net/entityframework6/code-first-insert-update-delete-stored-procedure-mapping.aspx
我想先使用 EF 代码导入存储过程,该代码是从 SQL 服务器创建的。我试图检查发布的文章,但到目前为止我似乎没有找到我理解的东西。下面的 SQL 语句是 3 个表的连接,所以如果我可以使用下面的示例代码获得如何解决它的示例。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InvoiceReport]
AS
SELECT
Ordering.order_id,
Ordering.CustomerId,
Ordering.invoice_number,
Ordering.date_order_placed,
Customer.FirstName,
Customer.LastName,
Customer.EmailId,
Invoice_Line_Items.item,
Invoice_Line_Items.department,
Invoice_Line_Items.service,
Invoice_Line_Items.gender,
Invoice_Line_Items.quantity,
Invoice_Line_Items.price,
Invoice_Line_Items.pick_up_address,
Invoice_Line_Items.pick_up_date,
Invoice_Line_Items.pick_up_time,
Invoice_Line_Items.drop_off_address,
Invoice_Line_Items.drop_off_date,
Invoice_Line_Items.drop_off_time
FROM
Ordering
JOIN
Customer ON Ordering.CustId = Customer.CustId
JOIN
Invoice_Line_Items ON Customer.CustId = Invoice_Line_Items.CustId
RETURN 0
我平时使用存储过程的方式如下:
- 添加 EF 代码第一次迁移(https://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx)
- 通过
Sql()
将存储过程添加到您的迁移中
- 使用模型构建器或在您的实体中配置存储过程
- 在您的代码中使用它:-)
迁移示例:
public partial class yourmigration: DbMigration
{
protected override void Up()
{
Sql("CREATE PROCEDURE SQL HERE");
}
//dont forget the down.
}
对于 EF 6,这里有一篇关于它的好文章:https://www.entityframeworktutorial.net/entityframework6/code-first-insert-update-delete-stored-procedure-mapping.aspx