【问题标题】:Reading stored procedure result with multiple tables using MVC EF使用 MVC EF 读取具有多个表的存储过程结果
【发布时间】:2021-03-30 10:42:10
【问题描述】:

我已经搜索过类似的问题,但它们都是旧的。

我正在尝试通过使用存储过程将所有相关表检索到一个以避免多次查询来优化我的工作。我想使用这种stored procedure,它返回多个表作为 JSON 和输出参数,但我是 ASP.NET Core MVC 和 EF 的新手。有人可以指导我正确的方向吗?如何读取该输出参数并将它们转换为对象?值得一提的是,我想为 dropLists 使用表。

主表有一些列提到了一些表。我想拥有这些表值并在一些下拉列表或复选框中显示它们。

【问题讨论】:

    标签: json entity-framework asp.net-core stored-procedures


    【解决方案1】:

    这不是很难,也许以后有人会问:

    var parameterReturn1 = new SqlParameter
                {
                    ParameterName = "FirstResult",
                    SqlDbType = System.Data.SqlDbType.VarChar,
                    Size = -1,
                    Direction = System.Data.ParameterDirection.Output,
                };
                var parameterReturn2 = new SqlParameter
                {
                    ParameterName = "SecondResult",
                    SqlDbType = System.Data.SqlDbType.VarChar,
                    Size= -1,
                    Direction = System.Data.ParameterDirection.Output,
                };
    
    _context.Database
                   .ExecuteSqlRaw("exec test @FirstResult output,@SecondResult output", parameterReturn1, parameterReturn2);
    
                var h2 = parameterReturn2.Value.ToString();      
                var list= JsonConvert.DeserializeObject<List<YourObj>>(h2);  
    

    在 Sp 我有这样的东西:

    CREATE PROCEDURE test2 
    @FirstResult NVARCHAR(MAX) OUT, @SecondResult NVARCHAR(MAX) OUT
    AS
    BEGIN
        SET NOCOUNT ON;
    
         SELECT @FirstResult =
          (
          SELECT *
            FROM table1
            WHERE customerID = 88164
          FOR JSON AUTO
          );
           SELECT @SecondResult =
          (
          SELECT *
            FROM table2
          
          FOR JSON AUTO
          );
    END
    GO
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      相关资源
      最近更新 更多