如何从实体框架传递参数到存储过程? [英] How to pass Parameters to Stored Procedure from Entity Framework?

查看:59
本文介绍了如何从实体框架传递参数到存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从实体框架向存储过程发送参数?预先感谢.

解决方案

第一个问题是:实体框架的哪个版本? .NET 3.5? .NET 4 ?? .NET 4中的情况已经发生了很大的变化(并且得到了改善!).

其次:您想做什么:

  • 从数据库中检索行

  • 执行没有返回值的存储过程

  • 将实体上的INSERT/UPDATE/DELETE操作映射到存储的proc?

这是三种截然不同的场景-因此我们需要知道您要做什么.

另外:只需使用Google(或Bing)进行搜索-有大量博客文章和教程向您展示了如何操作-快速列表:

从字面上看还有更多……

更新:好的,因此您想从数据库中检索数据.在这种情况下,您的步骤是:

  • 在设计器中转到您的EF模型(* .edmx文件)
  • 右键单击并选择Update Model from Database
  • 选择您要使用的存储过程并通过向导

这将为您的物理存储模型中的存储过程创建一个条目.下一个:

  • 转到Model Browser(请参阅上面的上下文菜单?它位于Update Model from Database下方),导航到存储模型"并找到您的过程
  • 右键单击该过程

  • 选择Add Function Import,它将物理存储模型中的功能"(存储过程)导入概念模型(基本上是您的实体上下文类).

在这里,您有四个选择:

  • 您存储的proc可能不会返回任何内容(例如在我的示例中)-但这只是上下文类中的一个方法,您可以调用该方法执行某些操作
  • 您存储的proc可能返回标量的集合,例如整数值或其他内容的列表-在下拉列表中选择适当的值
  • 您存储的过程可能会从您的模型中返回实体,例如完整的Customer实体-在这种情况下,选择最后一个选项,然后选择要映射的实体(在这种情况下,您存储的proc 必须返回该实体的所有列)

OR:

  • 您存储的proc返回的东西-但不仅是标量(不仅是INT),也不是实体-在这种情况下,您可以选择第三个选项并定义一个新的复杂类型(一个类),该类型将保存您从中返回的结果存储过程.

无论执行什么操作-基本上,EF都会在您可以调用的对象上下文类上创建一个方法.您存储的proc所需的任何参数都是该方法的参数,因此您可以轻松地传入例如字符串,整数等.

I want to know how to send parameters to stored procedure from entity framework? Thanks in advance.

解决方案

First question is: for which version of the Entity Framework?? .NET 3.5? .NET 4 ?? Things have significantly changed (and improved!) in .NET 4.

And secondly: what do you want to do:

  • retrieve rows from the database

  • execute a stored proc without return value

  • map INSERT/UPDATE/DELETE operations on an entity to a stored proc??

These are three pretty different scenarios - so we need to know what you're going for.

Also: just search with Google (or Bing) - there are plenty of blog post and tutorials out there showing you how to do it - a quick list:

and literally thousands more ......

Update: ok, so you want to retrieve data from the database. In that case, your steps are:

  • go to your EF model (*.edmx file) in the designer
  • right-click and select Update Model from Database
  • pick the stored procedure you want to use and go through the wizard

This creates an entry for the stored procedure in your physical storage model. Next:

  • go to the Model Browser (see the above context menu? It's just below Update Model from Database), navigate to the Storage Model and find your procedure
  • right-click on that procedure

  • Select Add Function Import which imports the "function" (stored procedure) from the physical storage model into the conceptual model (your entity context class, basically).

Here, you have four choices:

  • your stored proc might not return anything (like in my sample) - then it's just a method on your context class that you can call that does something
  • your stored proc might return a collection of scalars, e.g. a list of INT values or something - pick the appropriate value in the dropdown
  • your stored proc might return entities from your model, e.g. complete Customer entities - in that case, select the last option and pick the entity you want to map to (your stored proc must return all columns for that entity, in this case)

OR:

  • your stored proc returns something - but neither just scalars (not just INT), nor an entity - in that case, you can pick the third option and define a new complex type (a class) that will hold your results returned from the stored procedure.

Whichever you do - basically EF will create a method on your object context class that you can call. Any parameters your stored proc requires will be parameters of that method so you can very easily pass in e.g. strings, ints etc.

这篇关于如何从实体框架传递参数到存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆