如何首先使用代码导入存储过程ASP.NET MVC和实体框架

时间:2019-10-27 09:29:43

标签: asp.net asp.net-mvc-4 entity-framework-6 ef-code-first

我想先使用EF代码导入存储过程,该代码是从SQL Server创建的。我试图检查发布的文章,但似乎找不到到目前为止我了解的东西。下面的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

1 个答案:

答案 0 :(得分:0)

我通常通过以下方式使用存储过程:

迁移示例:

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