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

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

问题描述

我想知道如何从实体框架向存储过程发送参数?提前致谢.

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

推荐答案

第一个问题是:实体框架适用于哪个版本??.NET 3.5?.NET 4 ??.NET 4 中的事情发生了显着变化(和改进!).

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

其次:你想做什么:

  • 从数据库中检索行

  • retrieve rows from the database

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

execute a stored proc without return value

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

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.

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

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:

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

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

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

  • 选择Add Function Import,它将函数"(存储过程)从物理存储模型导入到概念模型(基本上是您的实体上下文类)中.
  • 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:

  • 您的存储过程可能不会返回任何内容(就像在我的示例中一样) - 那么它只是您的上下文类中的一个方法,您可以调用它来执行某些操作
  • 您的存储过程可能会返回一组标量,例如INT 值列表或其他值 - 在下拉列表中选择适当的值
  • 您的存储过程可能会从您的模型中返回实体,例如完整的 Customer 实体 - 在这种情况下,选择最后一个选项并选择要映射到的实体(您的存储过程 必须 返回该实体的所有列,在这种情况下)
  • 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)

或:

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

无论您做什么 - 基本上 EF 都会在您可以调用的对象上下文类上创建一个方法.您存储的过程需要的任何参数都将是该方法的参数,因此您可以非常轻松地传入例如字符串、整数等

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天全站免登陆