如何通过EF调用MVC中的存储过程 [英] How to call stored procedure in MVC by EF

查看:1370
本文介绍了如何通过EF调用MVC中的存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里可以得到实体框架很好的教程是存储过程的MVC框架?

Where can I get good tutorial on Entity framework with Stored Procedure in MVC framework?

它是更好的时候我几乎都写在存储过程中这种情况下,使用企业库。

Is it better to use Enterprise library in this case when I have almost everything written in the stored procedure.

请注意:我使用的存储过程,因为他们真的很复杂,其中有些是超过1000行

Note: I am using stored procedure because they are really very complex and some of them is over 1000 lines.

推荐答案

MVC在这种情况下,绝对没有关系的。你打电话是如何从EF存储过程的方式将仍然是相同的。我猜你想使用存储过程实际上并没有使用实体和LINQ到实体(主要EF功能),不是吗?一般来说,你需要:

MVC is in this case absolutely not related. The way how you call stored procedure from EF will be still the same. I guess you want to use stored procedures without actually using entities and linq-to-entities (main EF features), don't you? Generally you need:


  • 你来自哪里,数据库运行更新,并添加您要使用的所有存储过程EDMX文件(ado.net实体数据模型)。 EDMX文件也产生衍生的ObjectContext ,默认情况下所有实体。

  • 接下来你必须去模型浏览器,并为每个程序创建函数导入。功能导入将创建方法派生的ObjectContext 这将允许您调用存储过程与任何其他.NET方法。

  • 在功能导入你必须创建复杂的类型从存储过程返回的结果集(它可以自动发生)。

  • EDMX file (ado.net entity data model) where you run update from database and add all stored procedures you want to use. EDMX file also generates derived ObjectContext and all entities by default.
  • Next you must go to Model Browser and create Function import for each procedure. Function import will create method on the derived ObjectContext which will allow you call the stored procedure as any other .net method.
  • During function import you will have to create complex type (it can happen automatically) for result set returned from stored procedure.

您也不必使用功能的进口都和可以执行的程序直接通过调用:

You also don't have to use function imports at all and you can execute procedures directly by calling either:


  • objectContext.ExecuteSqlCommand(storedProcedureName,SqlParameters)用于SPS不返回记录集

  • objectContext.ExecuteStoreQuery<&与resultType GT(storedProcedureName,SqlParameters)用于SPS返回记录集。 与resultType 必须有相同的名字在结果集列的属性。它只能与平型(无嵌套对象)工作。

  • objectContext.ExecuteSqlCommand("storedProcedureName", SqlParameters) for SPs not returning record set
  • objectContext.ExecuteStoreQuery<ResultType>("storedProcedureName", SqlParameters) for SPs returning record set. ResultType must have properties with same names as columns in result set. It can work only with flat types (no nested objects).

有使用存储过程时,有一些限制:

There are some limitations when using stored procedures:


  • 实体框架不喜欢存储过程返回的动态结果集(基于某些条件的结果集都有不同的列)

  • 实体框架不支持存储过程返回多个结果集 - 有<一个href=\"http://blogs.infosupport.com/blogs/willemm/archive/2010/07/16/ado-net-entity-framework-advanced-scenarios-working-with-stored-procedures-that-return-multiple-resultsets.aspx\">EFExtensions这不,但它更像是直接做ADO.NET。

  • Entity framework doesn't like stored procedures which returns dynamic result sets (based on some condition result set has different columns)
  • Entity framework doesn't support stored procedures returning multiple result sets - there are EFExtensions which does but it is more like doing ADO.NET directly.

这篇关于如何通过EF调用MVC中的存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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