简体   繁体   中英

Passing parameter issue from Entity Framework to store procedure

I have app running in MVC and I am intended to send data from MVC app C# to Store Procedure. I have tested my stored procedure, it works on if I execute from SQL Server Management Studio, and in C# I am getting all the values from web page (view) to function where it is expecting to call store procedure so I am guessing I am doing something wrong in parameter passing!!!

Its not storing data in database

Many thanks in advance

Store Procedure

USE [MySolution01_DB]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[CreateNewFunctionsNavigation]
 @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

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; }    

}

Class to call Stored Procedure

public void CreateFunctionNavigation(CreateFunctionNavigation_SP_Map _entity)
    {

        using(var dbContext = new FunctionContext())
        {
            CreateFunctionNavigation_SP_Map modelObj = new CreateFunctionNavigation_SP_Map();

            var parameters = new[] { 
                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 )
            };


             dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("exec CreateNewFunctionsNavigation", parameters);
        }


    }

new update

I have change C# code but still not able to store data

dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("EXEC CreateNewFunctionsNavigation @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)
   );

You could have debugged this by using SQL Profiler which shows commands executed against the server. Anyways, try the following

dbContext.Database.SqlQuery("CreateNewFunctionsNavigation @FunctionName, @FunctionHierarchy_Level, @ControllerName, @ActionName, @Function_ParentsFunctionID", parameters);

Update

Since the answer above didnt work for you, you could try and create the query string manually like this..

        var query = string.Format("EXEC CreateNewFunctionsNavigation " +
                                  "@FunctionName = {0}, " +
                                  "@FunctionHierarchy_Level = {1}, " +
                                  "@ControllerName = {2}, " +
                                  "@ActionName = {3}, " +
                                  "@Function_ParentsFunctionID = {4}",
                                  _entity.FunctionName,
                                  _entity.FunctionHierarchy_Level, 
                                  entity.ControllerName,
                                  _entity.ActionName,
                                  _entity.Function_ParentsFunctionID);


        dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>(query);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM