数据库中有以下存储过程:
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.
说的是这个存储过程没有返回类型, 但是我不知道怎么定义它所说的返回类型。
这里虽然说也不是非要用返回值, 不过既然遇到了, 还是想解决掉。
原因有了, 我之前写数据库的时候, 没有最后的select语句, 然后就直接生成模型了, 然后后面改得数据, 我直接在模型上更新, 所以那些方法没有变。
只需要重新生成一遍模型就行了