存储过程仅使用Count执行

时间:2016-12-28 15:59:18

标签: .net stored-procedures model-view-controller code-first

我有一个存储过程,我称之为完全像我的其他人一样(工作正常)但它似乎只是在我添加Count()时执行...

这是我的控制器代码:

        public static void SaveGridLayout(string userName, string gridViewId, string layoutData)
    {
        using (var context = new Entities())
        {
            var userNameParam = new SqlParameter("@user_name", userName);
            var gridViewIdParam = new SqlParameter("@grid_id", gridViewId);
            var layoutParam = new SqlParameter("@layout", layoutData);

            context.Database.SqlQuery<UserGridLayoutsEntity>("sp_SaveLayout @user_name, @grid_id, @layout", userNameParam, gridViewIdParam, layoutParam);
        }
    }

这不会给出任何结果,但当我将最后一行更改为:context.Database.SqlQuery<UserGridLayoutsEntity>("sp_SaveLayout @user_name, @grid_id, @layout", userNameParam, gridViewIdParam, layoutParam).Count();时,它会执行并执行SP。不知何故不必添加计数并从一开始就执行过程将是很好的。 谢谢!

1 个答案:

答案 0 :(得分:1)

public static void SaveGridLayout(string userName, string gridViewId, string layoutData)
{
    using (var context = new Entities())
    {
        var userNameParam = new SqlParameter("@user_name", userName);
        var gridViewIdParam = new SqlParameter("@grid_id", gridViewId);
        var layoutParam = new SqlParameter("@layout", layoutData);

        context.Database.ExecuteSqlCommand("sp_SaveLayout @user_name, @grid_id, @layout", userNameParam, gridViewIdParam, layoutParam);
    }
}

我认为你可以像这样直接执行它。使用.SqlQuery甚至可以使用.ToList(),如果不是.Count()但是performccommand应该在你的情况下做得很好。