使用Entity Framework Core调用MYSQL存储过程并显示返回的数据 -

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

嗯,问题是自我解释。所以我不会不必要地扩展它。这是我的SP -

    CREATE procedure [dbo].[sp_TotalSalesByCustomerID]
    @customer_id nvarchar(50),
    @varSale decimal OUTPUT  
    as

    begin

    begin try

    if exists (
            Select * from tempdb..sysobjects Where id = 
       object_id('tempdb.dbo.#temp')
          )
        DROP TABLE #temp

    else

        select * into #temp from Orders where customer_id=@customer_id

        select @varSale=SUM(total_price) from #temp

    end try

    begin catch
           select error_message(),
           error_severity(),
           error_number(),
           error_state()    
    end catch

    end

如您所见,SP基本上只返回一个计算值。它没有映射到我的应用程序中的任何实体对象/类。我写的后端web api端的C#代码 -

[Route("TotalSalesByCustomerID")]
    [HttpPost]
    public JsonResult TotalSalesByCustomerID([FromBody]string customer_id)
    {
        try
        {
            //var salesT = (_appDbContext.Totals.FromSql("call sp_TotalSalesByCustomerID(@customer_id)", customer_id));
            var salesT= _appDbContext.Database.ExecuteSqlCommand("sp_TotalSalesByCustomerID @p0", parameters: new[] { customer_id });
            return Json(salesT);
        }
        catch(Exception exp)
        {
            return Json(exp.GetBaseException());
        }
    }

我不知道它返回的是什么codswallop垃圾。可能是因为它未映射到实体类对象。我明白那个。可能一些C#代码也是错误的。我也不需要单独的课程。这只是一个值!如果我添加一个类,我将被要求可能进行迁移,这在我的场景中是不必要的。仅供参考,这也是我的迁移构建器类,我在“向上和向下”中添加了我的sql代码 -

protected override void Up(MigrationBuilder migrationBuilder)
    {
        var sp = @"CREATE procedure [dbo].[sp_TotalSalesByCustomerID]
                    @customer_id nvarchar(50),
                    @varSale decimal OUTPUT  
                    as

                    begin

                    begin try

                    if exists (
                                Select * from tempdb..sysobjects Where id = object_id('tempdb.dbo.#temp')
                              )
                            DROP TABLE #temp

                    else

                            select * into #temp from Orders where customer_id=@customer_id

                            select @varSale=SUM(total_price) from #temp

                    end try

                    begin catch
                        select error_message(),
                               error_severity(),
                               error_number(),
                               error_state()    
                    end catch

                    end";

        migrationBuilder.Sql(sp);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        var sp = @"CREATE procedure [dbo].[sp_TotalSalesByCustomerID]
                    @customer_id nvarchar(50),
                    @varSale decimal OUTPUT  
                    as

                    begin

                    begin try

                    if exists (
                                Select * from tempdb..sysobjects Where id = object_id('tempdb.dbo.#temp')
                              )
                            DROP TABLE #temp

                    else

                            select * into #temp from Orders where customer_id=@customer_id

                            select @varSale=SUM(total_price) from #temp

                    end try

                    begin catch
                        select error_message(),
                               error_severity(),
                               error_number(),
                               error_state()    
                    end catch

                    end";

        migrationBuilder.Sql(sp);
    }

我不知道该怎么办。当然,我错过了一些很好的技巧或者一些黑客攻击。它是什么?我可以创建一些我可以在EF Core中使用的虚拟/抽象实体来获取我的数据而不是创建表,向我的数据库添加迁移等吗?还有什么选择?我期待着对此有一些很好的帮助。谢谢大家,

sql-server entity-framework entity-framework-core asp.net-core-webapi ef-migrations
1个回答
0
投票

您必须使用返回值的输出参数instdead,如下面的代码。

var outputParam = new SqlParameter("outputParam", SqlDbType.Int) { Direction = ParameterDirection.Output };
await _databaseContext.Database.ExecuteSqlCommand($"EXEC {storedProcedureName} @param1, @outputParam output", new SqlParameter("param1"), outputParam);

要获得值,您需要在两行之上调用outputParam.Valueafter。

ExecuteSqlCommand()的返回值是一个整数,它是受查询影响的记录数。

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