How to use DbContext.Database.SqlQuery(sql, params) with stored procedure? EF Code First CTP5

后端 未结 10 1777
梦谈多话
梦谈多话 2020-11-22 16:53

I have a stored procedure that has three parameters and I\'ve been trying to use the following to return the results:

context.Database.SqlQuery

        
10条回答
  •  名媛妹妹
    2020-11-22 17:49

    return context.Database.SqlQuery("mySpName {0}, {1}, {2}",
    new object[] { param1, param2, param3 });
    

    //Or

    using(var context = new MyDataContext())
    {
    return context.Database.SqlQuery("mySpName {0}, {1}, {2}",
    new object[] { param1, param2, param3 }).ToList();
    }
    

    //Or

    using(var context = new MyDataContext())
    {
    object[] parameters =  { param1, param2, param3 };
    
    return context.Database.SqlQuery("mySpName {0}, {1}, {2}",
    parameters).ToList();
    }
    

    //Or

    using(var context = new MyDataContext())
    {  
    return context.Database.SqlQuery("mySpName {0}, {1}, {2}",
    param1, param2, param3).ToList();
    }
    

提交回复
热议问题