【问题标题】:How to import stored procedure using code first ASP.NET MVC and Entity Framework如何使用代码优先 ASP.NET MVC 和实体框架导入存储过程
【发布时间】:2019-10-27 09:29:43
【问题描述】:

我想先使用 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

【问题讨论】:

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


    【解决方案1】:

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

    迁移示例:

    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

    【讨论】:

    • 能否举个例子。
    • MigrationBuilder 使用这个库 Microsoft.EntityFrameworkCore.Relational,我使用的是 ASP.NET MVC 4。所以它不支持。那么我可以使用任何替代方案吗? @Stefan
    • 啊,是的,对不起,我的错...我已经更新了答案,小改动;我添加了一篇文章。
    • 你有文章在线吗?如果你这样做,请分享链接
    猜你喜欢
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多