首页 新闻 会员 周边 捐助

ado.net entity中的存储过程调用的方法为什么是ExecuteFunction()而不是ExecuteFunction<TElement>

0
悬赏园豆:15 [已解决问题] 解决于 2013-10-09 18:26

数据库中有以下存储过程:

create proc NewUser
    @jobNo as varchar(35),
    @name as varchar(35),
    @type as nvarchar(20),
    @department as nvarchar(20),
    @message as nvarchar(500) output
as
begin
    set nocount on
    declare @count as int = 0
    if exists(select UserId from Users where JobNumber = @jobNo)
        begin
            set @message = N'用户已存在'
        end
    else
        begin
            declare @initialPwd as varchar(30) = '123456'
            declare @typeId as int = (select UserTypeId from UserType where UserTypeName = @type)
            declare @departId as int = (select DepartmentId from Department where DepartmentName = @department)
            begin try
                insert into ResearchUser
                (
                    JobNumber,
                    UserName,
                    UserPwd,
                    RU_DEPT_Id,
                    RU_UT_Id
                )
                values
                (
                    @jobNo,
                    @name,
                    @initialPwd,
                    @departId,
                    @typeId
                )
                set @count = @@ROWCOUNT
            end try
            begin catch
                set @message = (select error_message() as errorMessage)
            end catch
        end
    set nocount off
    select @count
end

该存储过程, 在ado.net实体中生成的代码如下:

public virtual int CreateUser(string jobNo, string userName, string userType, string department, ObjectParameter message)
        {
            var userJobNoParameter = jobNo!= null ?
                new ObjectParameter("jobNo", userJobNo) :
                new ObjectParameter("jobNo", typeof(string));
    
            var userNameParameter = userName != null ?
                new ObjectParameter("name", userName) :
                new ObjectParameter("name", typeof(string));
    
            var typeParameter = userType != null ?
                new ObjectParameter("type", userType) :
                new ObjectParameter("type", typeof(string));
    
            var departmentParameter = department != null ?
                new ObjectParameter("department", departName) :
                new ObjectParameter("department", typeof(string));
    
            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("CreateUser", userJobNoParameter, userNameParameter, typeParameter, departmentParameter, message);
        }

 

为什么这里最后调用的是ExecuteFunction()方法, 而不是ExecuteFunction<TElement>()方法? 然后手动把最后一句改成下面这样:

return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Nullable<int>>("NewUser", userJobNoParameter, userNameParameter, userTypeParameter, departNameParameter, message).FirstOrDefault().Value;

会报错, 错误信息如下:

The stored procedure or function 'CreateUser' does not have a return type. ExecuteFunction only supports stored procedures and functions that have a return type.

 说的是这个存储过程没有返回类型, 但是我不知道怎么定义它所说的返回类型。

这里虽然说也不是非要用返回值, 不过既然遇到了, 还是想解决掉。

< >
分享
最佳答案
0

原因有了, 我之前写数据库的时候, 没有最后的select语句, 然后就直接生成模型了, 然后后面改得数据, 我直接在模型上更新, 所以那些方法没有变。

只需要重新生成一遍模型就行了

hourglasser | 初学一级 |园豆:15 | 2013-10-09 18:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册