发送请求和参数时不调用存储过程

Stored Procedure is not called when send request along with parameters

我在 C# 中有一个 ASP.NET MVC 应用程序,我在其中调用存储过程 CreateFunctionNavigation。我在调用该存储过程和参数时遇到问题。我的模型 class 为;

Model class

public class CreateFunctionNavigation_SP_Map
{
    public CreateFunctionNavigation_SP_Map()
    {
    }

    [StringLength(250)]
    [Required(ErrorMessage = "Required Function Title")]
    [Display(Name = "Function Title")]
    public string FunctionName { get; set; }

    [Required(ErrorMessage = "Required Function Hierarchy; i.e Where Function Exists In Hierarchy Tree \n Top-Level Start From 1 ")]
    [Display(Name = "Function Hierarchy Level")]
    public int FunctionHierarchy_Level { get; set; }

    [StringLength(250)]
    [Required(ErrorMessage = "Required Controller Title")]
    [Display(Name = "Controller Title")]
    public string ControllerName { get; set; }

    [StringLength(250)]
    [Required(ErrorMessage = "Required Action Title")]
    [Display(Name = "Action Title")]
    public string ActionName { get; set; }

    [Required(ErrorMessage = "Required Function Parent - Child Relation ID \n Put 0 In Case Given Function doesn't Have Any Parent Function ")]
    [Display(Name = "Function Parent's FunctionID")]
    public int Function_ParentsFunctionID { get; set; }     
}

存储过程:

ALTER PROCEDURE [dbo].[CreateFunctionNavigation] 
 @FunctionName nvarchar(250),
 @Hierarchy_Level INT,
 @Function_identity INT OUTPUT, 
 @ControllerName nvarchar(250), 
 @Controller_identity INT OUTPUT,
 @ControllerInFunction_identity INT OUTPUT,
 @ActionName nvarchar(250),
 @Action_identity INT OUTPUT,
 @ActionInFunction_identity INT OUTPUT,
 @Function_ParentsFunctionID INT,
 @Function_ParentsFunction_identity INT OUTPUT
AS
BEGIN
  SET NOCOUNT ON;

  INSERT INTO [dbo].[Navigation_Functions] ([FunctionName],[Hierarchy_Level]) 
  VALUES(@FunctionName, @Hierarchy_Level)

  SET @Function_identity = SCOPE_IDENTITY()

  INSERT INTO [dbo].[Navigation_FunctionController] ([ControllerName])
  VALUES(@ControllerName)

  SET @Controller_identity = SCOPE_IDENTITY()

  INSERT INTO [dbo].[Navigation_FunctionInController] ([Function_ID], [ControllerID])
  VALUES (@Function_identity, @Controller_identity)

  SET @ControllerInFunction_identity = SCOPE_IDENTITY()

  INSERT INTO [dbo].[Navigation_FunctionAction] ([ActionName], [ControllerID])
  VALUES (@ActionName, @Controller_identity)

  SET @Action_identity = SCOPE_IDENTITY()

  INSERT INTO [dbo].[Navigation_FunctionInAction] ([ActionID], [Function_ID])
  VALUES (@Action_identity, @Function_identity)

  SET @ActionInFunction_identity = SCOPE_IDENTITY()

  INSERT INTO [dbo].[Navigation_FunctionHierarchy] ([Function_IDs], [Parent_Function_ID])
  VALUES (@Function_identity, @Function_ParentsFunctionID)

  SET @Function_ParentsFunction_identity = SCOPE_IDENTITY()

  RETURN
END

现在在 C# 中 class 我正在尝试 运行 这个带有传递参数的存储过程但是在 SQL Server Profiler 中我看不到这个存储过程是否没有被调用。

C# 代码 运行 存储过程

 var _result = dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("exec CreateFunctionNavigation @FunctionName @FunctionHierarchy_Level @ControllerName @ActionName @Function_ParentsFunctionID", 
                 new SqlParameter("FunctionName",_entity.FunctionName),
                 new SqlParameter("FunctionHierarchy_Level",_entity.FunctionHierarchy_Level),
                 new SqlParameter("ControllerName", _entity.ControllerName),
                 new SqlParameter("ActionName", _entity.ActionName),
                 new SqlParameter("Function_ParentsFunctionID",_entity.Function_ParentsFunctionID)

                 );

但是如果我 运行 这个存储过程只是通过不带参数调用它,当然只是简单的带有 select 语句的存储过程那么它就可以工作,我也可以在 SQL Profiler 中看到该存储过程被调用。

工作 C# 代码

List<CreateFunctionNavigation_SP_Map> query;
query = dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("exec CreateFunctionNavigation").ToList();

所以我认为问题出在 C# class 上,我试图通过参数调用 SP。我卡得很厉害,尝试了不同的选择,但不知道我做错了什么。非常感谢

您尝试过在参数名称之间添加逗号吗?

dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("exec CreateFunctionNavigation @FunctionName, @FunctionHierarchy_Level, @ControllerName, @ActionName, @Function_ParentsFunctionID"

更新:刚刚也检查了您的存储过程代码,发现其中存在一些问题。

下面是我将如何更改它

ALTER PROCEDURE [dbo].[CreateFunctionNavigation] 
 @FunctionName nvarchar(250),
 @Hierarchy_Level INT,    
 @ControllerName nvarchar(250), 
 @ActionName nvarchar(250),
 @Function_ParentsFunctionID INT,
 @Function_identity INT OUTPUT, 
 @Controller_identity INT OUTPUT,
 @ControllerInFunction_identity INT OUTPUT,
 @Action_identity INT OUTPUT,
 @ActionInFunction_identity INT OUTPUT,
 @Function_ParentsFunction_identity INT OUTPUT

这会将所有输出放在函数的末尾。

好的,在你的 C# 代码上

var function_identity new SqlParameter() {ParameterName = "Function_identity", Direction = ParameterDirection.Output};
var controller_identity new SqlParameter() {ParameterName = "Controller_identity", Direction = ParameterDirection.Output};
var controllerInFunction_identity new SqlParameter() {ParameterName = "ControllerInFunction_identity", Direction = ParameterDirection.Output};
var action_identity new SqlParameter() {ParameterName = "Action_identity", Direction = ParameterDirection.Output};
var actionInFunction_identity new SqlParameter() {ParameterName = "ActionInFunction_identity", Direction = ParameterDirection.Output};
var function_ParentsFunction_identity new SqlParameter() {ParameterName = "Function_ParentsFunction_identity", Direction = ParameterDirection.Output};

var _result = dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("exec CreateFunctionNavigation @FunctionName, @FunctionHierarchy_Level, @ControllerName, @ActionName, @Function_ParentsFunctionID, @Function_identity out, @Controller_identity out, @ControllerInFunction_identity out, @Action_identity out, @ActionInFunction_identity out, @Function_ParentsFunction_identity out", 
             new SqlParameter("FunctionName",_entity.FunctionName),
             new SqlParameter("FunctionHierarchy_Level",_entity.FunctionHierarchy_Level),
             new SqlParameter("ControllerName", _entity.ControllerName),
             new SqlParameter("ActionName", _entity.ActionName),
             new SqlParameter("Function_ParentsFunctionID",_entity.Function_ParentsFunctionID),
function_identity ,
controller_identity ,
controllerInFunction_identity ,
action_identity ,
actionInFunction_identity,
function_ParentsFunction_identity 

             );

这可能会让你接近某个地方。

这是我的答案;正在运行

 List<GetNewlyCreatedNavigationsFunction_SP_Map> _query;

             var function_identity_out = new SqlParameter("Function_identity", SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output };
             var controller_identity_out = new SqlParameter("Controller_identity", SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output };
             var controllerInFunction_identity_out = new SqlParameter("ControllerInFunction_identity", SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output };
             var action_identity_out = new SqlParameter("Action_identity", SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output };
             var actionInFunction_identity_out = new SqlParameter("ActionInFunction_identity", SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output };
             var function_ParentsFunction_identity_out = new SqlParameter("Function_ParentsFunction_identity", SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output };


             _query = dbContext.Database.SqlQuery<GetNewlyCreatedNavigationsFunction_SP_Map>("exec CreateFunctionNavigation @FunctionName, @Hierarchy_Level, @ControllerName, @ActionName, @Function_ParentsFunctionID, @Function_identity out, @Controller_identity out, @ControllerInFunction_identity out, @Action_identity out, @ActionInFunction_identity out, @Function_ParentsFunction_identity out",
                new SqlParameter("@FunctionName", _entity.FunctionName),
                new SqlParameter("@Hierarchy_Level", _entity.FunctionHierarchy_Level),
                new SqlParameter("@ControllerName", _entity.ControllerName),
                new SqlParameter("@ActionName", _entity.ActionName),
                new SqlParameter("@Function_ParentsFunctionID", _entity.Function_ParentsFunctionID),
                function_identity_out,
                controller_identity_out,
                controllerInFunction_identity_out,
                action_identity_out,
                actionInFunction_identity_out,
                function_ParentsFunction_identity_out 
              ).ToList();