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

问题描述 投票:0回答:1

我想使用代码优先导入存储过程,该代码是从SQL Server创建的。我试图检查发布的文章,但似乎找不到到目前为止我了解的东西。下面的SQL语句是3个表的联接,因此,如果我可以使用下面的示例代码来获得如何解决该问题的示例。

   USE [lifestyle]
   GO
   /****** Object:  StoredProcedure [dbo].[ReportMasterDetail]    Script Date: 2019/10/27 08:32:43 
   ******/
   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
asp.net asp.net-mvc-4 entity-framework-6 ef-code-first ef-code-first-mapping
1个回答
0
投票

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

  • 首先添加EF代码迁移
  • 将存储过程添加到通过Sql()进行的迁移中>
  • 使用模型构建器配置存储过程
  • 在您的代码中使用它:-)
  • 示例迁移:

public partial class yourmigration: Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.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

© www.soinside.com 2019 - 2024. All rights reserved.