执行命令时,参数必须是数据库参数或值

时间:2014-07-01 20:13:44

标签: c# entity-framework stored-procedures entity-framework-6 entity-framework-6.1

我无法检索ouput值。我得到以下异常:

执行类型:InvalidOperationException: 执行命令时,参数必须是数据库参数或值。

C#代码方法

public Student InsertNewStudentApplication(Student student)
{
    Student newStudent = null;
    SqlParameter returnParam = new SqlParameter()
    {
        ParameterName = "@newStudentID",
        SqlDbType = SqlDbType.Int,
        Value = 0,
        Direction = System.Data.ParameterDirection.Output
    };

    var result = m_context.Database.ExecuteSqlCommand("exec usp_MyProcedureName @newStudentID, @name, @schoolID, @localAdd, @localCity, @localState, @localZip, @summerAdd, @summerCity, @summerState, @summerZip, @phoneLocal, @phoneHome, @phoneCell, @email, @gradYear, @gradMajorMinor, @gradSchoolCollege, @TShirtSize, @transferStud, @prevExperience, @leadTransferStud, @fk_interviewID, @resumePath, @picturePath ",
    returnParam,
    new SqlParameter("@name", student.Name),
    new SqlParameter("@schoolID", student.schoolID),
    new SqlParameter("@localAdd", student.LocalAdd),
    new SqlParameter("@localCity", student.LocalCity),
    new SqlParameter("@localState", student.LocalState),
    new SqlParameter("@localZip", student.LocalZip),
    new SqlParameter("@summerAdd", student.SummerAdd),
    new SqlParameter("@summerCity", student.SummerCity),
    new SqlParameter("@summerState", student.SummerState),
    new SqlParameter("@summerZip", student.SummerZip),
    new SqlParameter("@phoneLocal", student.PhoneLocal),
    new SqlParameter("@phoneHome", student.PhoneHome),
    new SqlParameter("@phoneCell", student.PhoneCell),
    new SqlParameter("@email", student.Email),
    new SqlParameter("@gradYear", student.GradYear),
    new SqlParameter("@gradMajorMinor", student.GradMajorMinor),
    new SqlParameter("@gradSchoolCollege", student.GradSchoolCollege),
    new SqlParameter("@TShirtSize", student.TShirtSize),
    new SqlParameter("@transferStud", student.TransferStud),
    new SqlParameter("@prevExperience", student.PrevExperience),
    new SqlParameter("@leadTransferStud", student.LeadTransferStud),
    new SqlParameter("@fk_interviewID", SqlDbType.Int).Value =     student.InterviewID,
    new SqlParameter("@resumePath", student.ResumePath),
    new SqlParameter("@picturePath", student.PicturePath));

    var newStudentID = (int)returnParam.Value;
    if (newStudentID <= 0)
    {
        var tempStudent = new Student { ID = newStudentID };
        newStudent = this.FindByID(tempStudent);
    }

    return newStudent;
}

SQL存储过程:

ALTER PROCEDURE [dbo].[usp_MyProcedureName]
    @newStudentID       int out,
    @name               varchar(255),
    @schoolID               varchar(255),
    @localAdd           varchar(255),
    @localCity          varchar(50),
    @localState         varchar(50),
    @localZip           varchar(50),
    @summerAdd          varchar(255),
    @summerCity         varchar(50),
    @summerState        varchar(50),
    @summerZip          varchar(50),
    @phoneLocal         varchar(50),
    @phoneHome          varchar(50),
    @phoneCell          varchar(50),
    @email              varchar(255),
    @gradYear           varchar(50),
    @gradMajorMinor     varchar(255),
    @gradSchoolCollege  varchar(255),
    @TShirtSize         varchar(50),
    @transferStud       varchar(50),
    @prevExperience     text,
    @leadTransferStud   varchar(50),
    @fk_interviewID     int,
    @resumePath         varchar(255),
    @picturePath        varchar(255)
AS

SET NOCOUNT OFF;

BEGIN
    INSERT INTO OApp_students
    (
        name,
        schoolID,
        localAdd,
        localCity,
        localState,
        localZip,
        summerAdd,
        summerCity,
        summerState,
        summerZip,
        phoneLocal,
        phoneHome,
        phoneCell,
        email,
        gradYear,
        gradMajorMinor,
        gradSchoolCollege,
        TShirtSize,
        transferStud,
        prevExperience,
        leadTransferStud,
        fk_interviewID,
        resumePath,
        picturePath 
    )
    VALUES
    (
        @name,
        @schoolID,
        @localAdd,
        @localCity,
        @localState,
        @localZip,
        @summerAdd,
        @summerCity,
        @summerState,
        @summerZip,
        @phoneLocal,
        @phoneHome,
        @phoneCell,
        @email,
        @gradYear,
        @gradMajorMinor,
        @gradSchoolCollege,
        @TShirtSize,
        @transferStud,
        @prevExperience,
        @leadTransferStud,
        @fk_interviewID,
        @resumePath,
        @picturePath
    )

    set @newStudentID = @@IDENTITY

END

1 个答案:

答案 0 :(得分:1)

参数@newStudentID应该是输出参数(Direction = ParameterDirection.Output)。但是,这里更好的模式是通过

来结束
SELECT SCOPE_IDENTITY() as NewStudentID;

代替set @newStudentID = @@IDENTITY。并删除@newStudentID参数。部分as NewStudentID是可选的。现在你可以通过

调用sproc了
var newStudentId = m_context.Database
                            .SqlQuery<int>("exec usp_MyProcedureName @name, ...)
                            .First();

顺便提一下,您是否知道将存储过程直接映射到实体的插入,更新和删除操作的可能性?