[转]Executing a stored procedure with an output parameter using Entity Framework
2010-03-10 08:46 AnyKoro 阅读(604) 评论(0) 编辑 收藏 举报Entity Framework does support stored procedures with output parameters.
It's simple, as you will see in my code example below.
Allow me to debunk the widespread misconceptions about support in EF for stored procs with output params. A search for relevant keywords today yields almost 70,000 results in Google. There are thousands of web pages where people have asked how to do this. The top result was the MSDN forum, where the accepted answer to this question says "Sorry to give you bad new but EF does not support output parameters" and proposes reverting to conventional ADO.NET for this scenario, which is fine, but overlooks this feature of EF. Other typical answers from the top ten results include "in short: do not use entity framework yet", etc.
There's no reason why you would ever need to use an output parameter to return a value from a stored procedure with Entity Framework. However, if you want to do so, e.g. to gain the benefits of EF with a legacy database, there are various ways to harness this capability in Entity Framework. I'll demonstrate the simplest method here. (EF can provide several important benefits, e.g. improved performance; easier, more rapid development; etc.)
Example: using Entity Framework to execute a stored procedure and get the output parameter
var model = newBlogEntities();
var newIds = model.proc_BlogPostInsert(idParameter, "Post title");
int newId = newIds.FirstOrDefault().Id;
- Create a table called NewId containing one column named "Id" of type int.
- Update your model from the database. This gives you a NewId entity that you can use to get the Id of newly created records in any table with a primary key named "Id".
- Add one line of T-SQL to the end of your stored procedure to return an entity:
SELECT @Id = SCOPE_IDENTITY() AS Id; - You can then get the primary key of the newly created record either from the output parameter of the stored procedure, or from the returned entity. (I do both in my example, but obviously you'll want to remove the unnecessary extra code.) The latter is in line with standard practice in ORM, and hence more consistent with the way EF maps database objects to application entities.
In conclusion, it's easy to get the value of an output parameter of a stored procedure using EF. In new development projects where we want to adopt an ORM architecture, we have no reason to use output parameters in the way we might have previously. However, I do request that Microsoft adds more features to EF for handling stored proc output params for developers who want to use EF with legacy databases which weren't designed with ORM principles in mind.
23 July 2009
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2009-03-10 DinnerNow.Net中的AJAX应用体味(3)
2009-03-10 DinnerNow.Net中的AJAX应用体味(2)