如何在Entity Framework中使用相同的存储过程插入,更新和删除?

时间:2015-10-19 06:26:06

标签: sql-server entity-framework stored-procedures

我的问题一样this。但我不理解解决方案。在我的SP参数" @ OperationType"确定什么是操作类型。(如果是1然后插入,如果是2然后更新,如果是3然后删除) 我的存储过程是这样的:

ALTER PROCEDURE [dbo].[JobOperation] (
@ID                       INT = NULL OUTPUT,
@JobTitle                 NVARCHAR(50) = NULL,
@JobLevel                 NVARCHAR(50) = NULL,
@Des                      NVARCHAR(MAX) = NULL,
@IsDbCommandCommitted     BIT = 0 OUTPUT,
@DbCommitError            VARCHAR(200) = NULL OUTPUT,
@OperationType            INT = NULL,
@LanguageID               INT = NULL
)
AS
IF @operationType = 1
BEGIN
    BEGIN TRY
        BEGIN TRANSACTION
        INSERT INTO ....

        SELECT @ID = MAX(ID)
        FROM   JOB

        SET @IsDbCommandCommitted = 1
        COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
        SET @DbCommitError = ERROR_MESSAGE()
        ROLLBACK TRANSACTION
    END CATCH
END
ELSE 
IF @OperationType = 2
BEGIN
    BEGIN TRY
        BEGIN TRANSACTION
        UPDATE JOB
        ......

        SET @IsDbCommandCommitted = 1
        COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
        SET @DbCommitError = ERROR_MESSAGE()
        ROLLBACK TRANSACTION
    END CATCH
END
ELSE 
IF @OperationType = 3
BEGIN
    BEGIN TRY
        BEGIN TRANSACTION
        DELETE 
        FROM   JOB
        WHERE  ID = @ID

        SET @IsDbCommandCommitted = 1
        COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
        SET @DbCommitError = ERROR_MESSAGE()
        ROLLBACK TRANSACTION
    END CATCH
END

任何想法?

1 个答案:

答案 0 :(得分:1)

Pass parameter Using EF

按照本教程使用EF将参数传递给SP,这应该可以解决您的查询