实体框架存储过程问题

时间:2013-04-12 10:16:13

标签: entity-framework stored-procedures edmx

我定义了以下存储过程:

USE [BcmMetrice]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ActivityAdd_proc] @Name nvarchar(max),@Description nvarchar(max)  =null ,@Users nvarchar(max),@Object_id nvarchar (15) =null, @Source nvarchar(10)  =null, @TemplateId bigint  =null, @UserID bigint =null
AS


DECLARE activityUsers_cursor CURSOR FOR
select s from dbo.SplitString(@Users, ';')


DECLARE
@new_ActivityId bigint,
@new_CommentId bigint,
@activityUser_l bigint

BEGIN TRY

INSERT INTO [BcmMetrice].[dbo].[Activity]
           ([Name]
           ,[Description]
           ,[Type]
           ,[Created])
     VALUES
           (@Name
           ,@Description
           ,ISNULL(@TemplateId,0)
           ,GETDATE())

SET @new_ActivityId = (SELECT SCOPE_IDENTITY())

INSERT INTO [BcmMetrice].[dbo].[Comment] ([UserID],[CommentText],[Timestamp])
VALUES (ISNULL(@UserID,151),'Activity has been created',GETDATE())

SET @new_CommentId = (SELECT SCOPE_IDENTITY())

INSERT INTO [BcmMetrice].[dbo].[ActivityComment] ([ActivityID],[CommentID])
VALUES (@new_ActivityId, @new_CommentId)

INSERT INTO [BcmMetrice].[dbo].[Log]([Timestamp],[Type],[Data],[StackTrace]) VALUES (GETDATE(),'SQL.ActivityAdd_proc','users='+ISNULL(CAST(@Users as varchar(max)),'empty'),null)

OPEN activityUsers_cursor
FETCH NEXT FROM activityUsers_cursor INTO @activityUser_l
WHILE @@FETCH_STATUS = 0
    BEGIN

        INSERT INTO [BcmMetrice].[dbo].[Log]([Timestamp],[Type],[Data],[StackTrace]) VALUES (GETDATE(),'SQL.ActivityAdd_proc','Inserting users='+ISNULL(CAST(@activityUser_l as varchar(max)),'empty'),null)

        INSERT INTO [BcmMetrice].[dbo].[ActivityUser]
               ([ActivityId]
               ,[UserId]
               ,[Role])
         VALUES
               (@new_ActivityId
               ,@activityUser_l
               ,1)

        FETCH NEXT FROM activityUsers_cursor INTO @activityUser_l
    END

CLOSE activityUsers_cursor
DEALLOCATE activityUsers_cursor

END TRY

BEGIN CATCH

PRINT 'ERROR'
INSERT INTO [BcmMetrice].[dbo].[Log]([Timestamp],[Type],[Data],[StackTrace]) VALUES (GETDATE(),'SQL.ActivityAdd_proc','ERROR CATCHED!'+ERROR_MESSAGE(),null)


END CATCH

select @new_ActivityId

我想要做的是从程序返回新添加的活动的id。这就是为什么在我使用的那一行:

select @new_ActivityId

在SQL Management Studio中测试此过程时,一切似乎都正常。当我尝试在.NET项目中使用此过程时,问题就开始了。我更新了我的edmx模型表单数据库,但是当我执行此过程时,返回值无效。

程序执行如下所示:

int ret = dc.Db.ActivityAdd_proc(name, description, users, object_id, source, templateId, userId);

有没有人知道我可能做错了什么?

1 个答案:

答案 0 :(得分:1)

我找到了解决问题的方法。当您尝试通过select语句从存储过程返回数据时,必须在edmx的模型浏览器中创建函数导入。在向导中,您选择一种返回的集合(在我的例子中,它是int64类型的标量)。