【发布时间】:2015-01-26 11:15:07
【问题描述】:
我在 C# 中有一个 ASP.NET MVC 应用程序,我在其中调用存储过程 CreateFunctionNavigation。我在调用该存储过程和参数时遇到问题。我有模型类;
Model类
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# 类中,我尝试使用传递参数运行此存储过程,但在 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();
所以我认为问题是我试图从其中调用 SP 以及参数的 C# 类。我严重卡住了,尝试了不同的选择,但不知道我做错了什么。非常感谢提前
【问题讨论】:
标签: c# asp.net-mvc entity-framework stored-procedures