3

我想将存储过程输出参数映射到实体。

例如,

PROCEDURE ForExample
@ID int,
@LastUpdate datetime OUTPUT
AS
Update EntityTable Set LastUpdate = GETDATE() Where ID = @ID
Select @LastUpdate = LastUpdate From EntityTable Where ID = @ID

我想将 @LastUpdate 输出参数映射到实体属性。

在“存储过程映射”对话框中,@LastUpdate 参数显示为 InOut 参数(绿色箭头双向)。无论我是否将参数映射到属性,我都会收到相同的错误:

映射函数绑定指定具有不受支持参数的函数 Model.Store.ForExample:LastUpdate。输出参数只能通过 RowsAffectedParameter 属性进行映射。使用结果绑定从函数调用返回值。

我尝试在“结果列绑定”中为 LastUpdate 添加手动绑定,但这不起作用。

EF 4 是否支持我正在尝试做的事情,如果支持,它是如何完成的?

4

2 回答 2

1

As it's said in the error text, "Output parameters may only be mapped through the RowsAffectedParameter property. Use result bindings to return values from a function invocation"

If you want your procedure to return some value, e.g. last update, it should return it via a result set. That means, your stored procedure should look like this:

PROCEDURE ForExample
@ID int,
AS
Update EntityTable Set LastUpdate = GETDATE() Where ID = @ID
/*return lastupdate in resultset*/
Select LastUpdate as LastUpdate From EntityTable Where ID = @ID

And than you should map your result set column to lastupdate property like this: enter image description here

And stored procedure's output parameters can only be used for optimistic concurrency.

于 2014-01-26T10:48:22.810 回答
0

这是另一种使用代码而不是设计器的方法:http: //blogs.msdn.com/b/diego/archive/2012/01/10/how-to-execute-stored-procedures-sqlquery-in-the-dbcontext -api.aspx

于 2014-01-26T11:08:54.193 回答