用于存储过程的EF 6 .Database.SqlQuery只返回存储过程的字符串

时间:2014-03-17 16:11:59

标签: entity-framework stored-procedures

返回的结果只是传递给SQLQuery的字符串......

             var beginDt = new DateTime(2014, 03, 17);
                string beginDtStr = beginDt.ToString("yyyy-MM-dd");
                var callParams = new object[]
                {
                    dbName
                    , true
                    , 4
                    , 5 //   @EntityAffiliateTypeID 
                    , "Test Name "
                    , "Empty Note" /
                    , "5192223333"
                    , "5193334444"
                    , "bing@bing.com"
                    , "59 London Rd"
                    , null
                    , 8
                    , "Sarnia"
                    , 53
                    , "N7T2B1"
                    , 6
                    , 2
                    , null
                    , null
                    , null
                    , beginDtStr
                    , null
                    , "Another Testname"
                    , "5192223333"
                    , "5193334444"
                    , "email@domain.com"
                    , null
                    , null
                };

              var result2 = db.Database.SqlQuery<int>(
                    "EXEC SPEntityAffiliateInsert {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25}, {26}, {27}",
                    callParams
                    );

当您检查result2时,它恰好是:

"EXEC SPEntityAffiliateInsert {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25}, {26}, {27}"

就像你为字符串制作select语句一样:

SELECT 'EXEC SPEntityAffiliateInsert {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25}, {26}, {27}';

那么,如何让它实际调用sproc?

(我已经尝试过:                   var result2 = db.Database.SqlQuery(                         &#34; SPEntityAffiliateInsert {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{ 11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23} ,{24},{25},{26},{27}&#34;,

和                   var result2 = db.Database.SqlQuery(                         &#34; EXEC SPEntityAffiliateInsert {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14 } {15} {16} {17} {18} {19} {20} {21} {22} {23} {24} {25} {26} {27}&#34;, ) 任何帮助表示赞赏

1 个答案:

答案 0 :(得分:1)

这里的一个问题是调用SP的常规方法不起作用;此SP将Return语句用于单个整数值。所有正常的EF SP调用约定都期望IEnumerable兼容的结果,所以我必须采取不同的行为 - 我不被允许更新SP。

字符串未被解释为命令,因为SP未被架构限定。我还将其更新为与在Query窗口中运行完全相同的运行:

var commandSQL = "DECLARE @return_value int; " +
      "EXEC @return_value = [dbSchemaName].[SPEntityAffiliateInsert] " +
       "@ParamXName = {0} ; SELECT 'Return Value' = @return_value;";

然后叫它:

   IEnumerable<int> results = db.Database.SqlQuery<int>(commandSQL, strParams.ToArray()).ToList();
                    foreach (int result in results)
                    {
                        model.expectedresultsfromDBfield = result;
                        //only looking for one result...(could have called FirstOrDefault, I suppose...
                        break;
                    }

一切正常

相关问题