我如何让实体框架识别动态存储过程中的列
我正在使用 Entity Framework 4,我们的模型中有一堆存储过程。目前我们可以做我们需要做的一切。然而,我们有一个新过程,它接受一个字符串,并最终执行一些操作,例如
Create Procedure usp_RunSearch
@searchTerm VARCHAR(2000)
AS
BEGIN
DECLARE @sql VARCHAR(4000)
SET @sql = '
SELECT ID,
NAME
FROM Users'
IF(ISNULL(@searchTerm, '') != '')
BEGIN
SET @sql = @sql + 'WHERE ' + @searchTerm
END
Exec (@sql)
END
返回结果集。
EF 似乎无法询问此过程以获取结果列列表。
我能做些什么来帮助 EF 克服这个障碍吗?
I am using Entity Framework 4 and we have a bunch of stored procedures in our model. Currently we can do everything we need. However we have a new procedure that takes a string, and ultimately performs something such as
Create Procedure usp_RunSearch
@searchTerm VARCHAR(2000)
AS
BEGIN
DECLARE @sql VARCHAR(4000)
SET @sql = '
SELECT ID,
NAME
FROM Users'
IF(ISNULL(@searchTerm, '') != '')
BEGIN
SET @sql = @sql + 'WHERE ' + @searchTerm
END
Exec (@sql)
END
to return the result-set.
EF does not seem to be able to interrogate this procedure to get the resultant column list.
Is there anything i can do to help EF get over this hurdle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你只是想使用它,你可以这样做。
检查此链接。
顺便说一句,尝试为存储过程参数添加默认值,这也可能有效。
If you just want to use it, you can do so.
check this link out.
http://blogs.msdn.com/b/adonet/archive/2011/02/04/using-dbcontext-in-ef-feature-ctp5-part-10-raw-sql-queries.aspx
Btw, try adding a default value for the stored procedure parameter, that might also work.
最后,我在 EDMX 中手动创建了一个 ComplexType,并为存储过程设置了函数导入以使用这个手动定义的复杂类型。我认为,当 EF 检查存储过程时,它无法计算出返回的列集,即使在我的示例中选择列表相对静态。
In the end i have created a ComplexType by hand in the EDMX and setup the function import for the stored procedure to use this manually defined complex type. I think that when EF inspects the sproc it can't figure the return set of columns, even though the select list is relatively static in my example.