问:


I recently migrated from EF Core 2.2 to EF Core 3.0.
Unfortunately, I haven't found a way to call a stored procedure that returns an entity.
In EF Core 2.0 it was possible:

var spParams = new object[] { "bla", "xx" };
var createdPath = ModelContext.Paths.FromSql("AddNodeWithPathProc @p0, @p1", spParams).Single();

In EF Core 3.0 the method FromSQL is replaced with FromSqlRaw. However, I didn't manage to successfully call a stored procedure and then process the value. This is useful when the stored procedure inserts data into the database.
So in EF Core 3.0, I use this code:

var createdPath = ModelContext.Paths.FromSqlRaw("AddNodeWithPathProc @p0, @p1", spParams).Single();

but it will throw an exception, because the generated SQL is invalid and looks something like this:

exec sp_executesql N'SELECT TOP(2) [p].[PathId], [p].[Level], [p].[NodeId], [p].[NodePath], [p].[NodePathString]
FROM (
@sql @p0, @p1
) AS [p]',N'@p0 nvarchar(4000),@p1 nvarchar(4000), @sql nvarchar(100)',@p0=N'1a',@p1=N'', @sql=N'AddNodeWithPathProc'

I tried quite a few variations, but without success.
I'm starting to think that it is not possible to run stored procedures with ModelContext.[IQueryable].FromSqlRaw. In my opinion this kind defeats one of the major reasons for FromSqlRaw because, for normal select statements, LINQ is normally good enough.
Does anyone know how to use stored procedures in combination with FromSqlRaw in EF Core 3.0? Any help is greatly appreciated.
Thanks in advance
PS: I know you can execute a stored procedure with this.Database.ExecuteSqlRaw(SQL, parameters). However, that way it is not possible retrieve any entities that the stored procedure queries.

答:


Try .ToList() instead of .Single(). .Single() is generating the "TOP(2)" wrapper.

var createdPath = ModelContext.Paths.FromSqlRaw("AddNodeWithPathProc  {0}, {1}", nodeTitle, parentPathString).ToList();

原文链接

最新文章

  1. 如何获得Webapp的根项目路径 即ServletContext.getRealPath() 的输入参数要以"/"开头
  2. MSDN杂志上Wix相关的文章
  3. 20款风格独特的搜索框 PSD 设计素材免费下载
  4. Mongo中append方法使用
  5. Linux及安全课程——相关链接总结
  6. Win7-64bit系统下安装mysql的ODBC驱动
  7. poj1222
  8. Struts2 技术全总结 (正在更新)
  9. MAC地址格式小结
  10. android工具类常用方法
  11. [算法题] Search in Rotated Sorted Array ii
  12. Linux SHELL中sh和bash的区别
  13. mongodb ISODate问题(大量数据update优化)
  14. C++ new和delete 堆和栈
  15. PHPWeb开发相关知识
  16. Populating Next Right Pointers in Each Node II leetcode java
  17. java 事件通告写法
  18. ballerina 学习 三十 扩展开发(一)
  19. WordPress使用淘宝IP地址库的API显示评论者的位置信息(二)
  20. HTTP接口开发专题四(接收http接口发送过来的请求)

热门文章

  1. 18、Apache服务器
  2. scanf的返回值
  3. c# 第一节 .net与c#区分
  4. 201871010106-丁宣元 《面向对象程序设计(java)》第十一周学习总结
  5. django学习-视图练习
  6. 03-cmake语法-变量,字符串
  7. Fedora增加rc-local服务开机自启项
  8. 在WEB显示实时视频流
  9. openlayers绘制点,线,圆等
  10. vue-quill-editor富文本编辑器,添加了汉化样式却汉化不了