EF6调用存储过程,返回多个结果集处理
链接:http://www.codeproject.com/Articles/675933/Returning-Multiple-Result-Sets-from-an-Entity-Fram
案例:下载
Create Visual Studio Project with Entity Data Model
Details to create an entity database-first app project are described in the MSDN tutorial. Follow the steps for building the app except using the sample database described above, the StoreDBModel
for the model name, and the StoreDBEntities
for the database connection string name.
Add Stored Procedures into the Entity Data Model
<FunctionImport Name="GetAllCategorisAndProducts" ReturnType="Collection(StoreDBModel.Category_SprocResult)" />
<FunctionImport Name="GetProductsCM" ReturnType="Collection(StoreDBModel.Product_SprocResult)" />
Change the code in the first FunctionImport
node as shown below. This is actually to merge the second return type into the first FunctionImport
node. The Type
attribute in the second ReturnType
node can easily be copied from the second FunctionImport
node. After the merging, just leave the second FunctionImport
node there as it will automatically be deleted when we do the clean-up from the Model Browser later.
<FunctionImport Name="GetAllCategorisAndProducts">
<ReturnType Type="Collection(StoreDBModel.Category_SprocResult)" />
<ReturnType Type="Collection(StoreDBModel.Product_SprocResult)" />
</FunctionImport>
<FunctionImport Name="GetProductCM"
ReturnType="Collection(StoreDBModel.Product_SprocResult)" />
<FunctionImportMapping FunctionImportName="GetAllCategorisAndProducts"
FunctionName="StoreDBModel.Store.GetAllCategorisAndProducts">
<ResultMapping>
<ComplexTypeMapping TypeName="StoreDBModel.Category_SprocResult">
<ScalarProperty Name="CategoryID" ColumnName="CategoryID" />
<ScalarProperty Name="CategoryName" ColumnName="CategoryName" />
<ScalarProperty Name="ProductCount" ColumnName="ProductCount" />
</ComplexTypeMapping>
</ResultMapping>
</FunctionImportMapping>
<FunctionImportMapping FunctionImportName="GetProductCM" FunctionName="StoreDBModel.Store.GetProductCM">
<ResultMapping>
<ComplexTypeMapping TypeName="StoreDBModel.Product_SprocResult">
<ScalarProperty Name="ProductID" ColumnName="ProductID" />
<ScalarProperty Name="ProductName" ColumnName="ProductName" />
<ScalarProperty Name="CategoryID" ColumnName="CategoryID" />
<ScalarProperty Name="StatusCode" ColumnName="StatusCode" />
<ScalarProperty Name="StatusDescription" ColumnName="StatusDescription" />
<ScalarProperty Name="UnitPrice" ColumnName="UnitPrice" />
<ScalarProperty Name="AuditTime" ColumnName="AuditTime" />
</ComplexTypeMapping>
</ResultMapping>
</FunctionImportMapping>
Add the ResultMapping
node from the second FunctionImportMapping
into the firstFunctionImportMapping
. Leave the entire second FunctionImportMapping
node there for now.
<FunctionImportMapping FunctionImportName="GetAllCategorisAndProducts"
FunctionName="StoreDBModel.Store.GetAllCategorisAndProducts">
<ResultMapping>
<ComplexTypeMapping TypeName="StoreDBModel.Category_SprocResult">
<ScalarProperty Name="CategoryID" ColumnName="CategoryID" />
<ScalarProperty Name="CategoryName" ColumnName="CategoryName" />
<ScalarProperty Name="ProductCount" ColumnName="ProductCount" />
</ComplexTypeMapping>
</ResultMapping>
<ResultMapping>
<ComplexTypeMapping TypeName="StoreDBModel.Product_SprocResult">
<ScalarProperty Name="ProductID" ColumnName="ProductID" />
<ScalarProperty Name="ProductName" ColumnName="ProductName" />
<ScalarProperty Name="CategoryID" ColumnName="CategoryID" />
<ScalarProperty Name="StatusCode" ColumnName="StatusCode" />
<ScalarProperty Name="StatusDescription" ColumnName="StatusDescription" />
<ScalarProperty Name="UnitPrice" ColumnName="UnitPrice" />
<ScalarProperty Name="AuditTime" ColumnName="AuditTime" />
</ComplexTypeMapping>
</ResultMapping>
</FunctionImportMapping>
<FunctionImportMapping...>
. . .
</FunctionImportMapping>
- Open the EF designer by clicking the StoreDBModel.edmx file on the Solution Explorer. Then right-click any blank area on the designer and select the Update Model from Database…. This will open the Update Wizard window.
- Select the two stored procedures from the Stored Procedures and Functions list in the Add tab. Make sure that the Import selected stored procedures and functions into the entity model is checked and then click the Finish button. This will automatically add the function import mappings and the complex types for the stored procedures.
- Right-click any blank area on the EF designer and select the Model Browser. In the Model Browser, change the Complex Type name
GetAllCategorisAndProducts_Result
toCategory_SprocResult
, and theGetProductCM_Result
toProduct_SprocResult
as shown below. - Save the StoreDBModel.edmx file. The two complex type objects are created with the names we changed. Now we need to manually edit the XML content of the StoreDBModel.edmx file. Right-clicking the file, select the Open With…, and then XML (Text) Editor. Find the
FunctionImport
nodes under theedmx:ConceptualModels
node: - Find the
FunctionImportMapping
nodes under theedmx:Mappings/../En<FunctionImportMapping
node. - Clean up dummy stored procedure settings by opening the Model Browser again. Delete the
GetProductCM
in both Stored Procedures/Functions and Function Import lists. This will automatically delete all settings for theGetProductCM
stored procedure mappings and also the method to call the dummy stored procedure in the StoreDBModel.Context.cs file. - Save the StoreDBModel.edmx file. All changes in settings and clean-up will then be in effect.
What Happen When Updating Model
Will the manual editing for returning multiple result sets from the stored procedure be overwritten when updating the model due to any database schema changes? Based on results of my tests using the Visual Studio 2013, all the editing changes were kept intact when adding entities or other stored procedures into, or deleting any items from, the model except for deleting or refreshing the edited stored procedure mappings.
When adding or changing the input/output parameters in the stored procedure, the updates will automatically be refreshed in the model if executing the Refresh tab from the Update Model from Database (Update Wizard) screen. For example, adding @
as an input parameter to the stored procedure,Test
nvarchar(50)
GetAllCategorisAndProducts
, in the database then refreshing the model will insert the Parameter
node into the stored procedure’s FunctionImport
node even though it was manually edited before.
<FunctionImport Name="GetAllCategorisAndProducts">
<ReturnType Type="Collection(StoreDBModel.Category_SprocResult)" />
<ReturnType Type="Collection(StoreDBModel.Product_SprocResult)" />
<Parameter Name="Test" Mode="In" Type="String" />
</FunctionImport>
Automatic refreshing stored procedure complex type mappings due to changes in returning fields is not supported in any version of the EF, even for a stored procedure returning a single result set. We need to either re-add the stored procedure to the model after dropping the stored procedure and function import mappings from the model, or manually update the complex type using the Model Browser or the XML editor.
EF6调用存储过程,返回多个结果集处理的更多相关文章
- myabatis oracle 调用存储过程返回list结果集
Mapper.xml 配置 <resultMap type="emp" id="empMap"> <id property="emp ...
- PostgreSQL 调用存储过程返回结果集
创建返回结果集类型的存储过程: CREATE OR REPLACE FUNCTION public.f_get_member_info( id integer, productname charact ...
- jdbc调用存储过程获取多个结果集
jdbc调用存储过程获取多个结果集 2017年07月26日 21:20:22 Kenny-Liu 阅读数:1486 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- Java 调用存储过程 返回结果集
这里使用Oracle数据库的thin连接. 下面是存储过程SQL 1 createorreplaceprocedure proc3(stid in student.stuid%type, stname ...
- mybatis 调用存储过程 返回游标 实例
存储过程示例: create or replace procedure Fsp_Plan_CheckPrj(v_grantno varchar2, v_deptcode number, v_curso ...
- sqlserver,获取调用存储过程返回数据的方法。
1,获取存储过程最后select返回的结果集.SELECT 数据集返回值. 因为select返回的结果是一个表.所以返回的结果需要用一个表接收.使用临时表接收. 被调用的存储过程最后是这样:返回了一个 ...
- 整理sqlserver 级联更新和删除 c#调用存储过程返回值
整理一下级联更新和删除 c#调用返回值 use master go IF exists(select 1 from sysdatabases where name='temp') BEGIN DROP ...
- java调用oracle存储过程返回多条结果集
oracle版本:11g oracle存储过程,使用游标的方式返回多行.多列数据集合: CREATE OR REPLACE PROCEDURE SP_DATA_TEST( /*P_ID IN INT, ...
- [转].net 调用oracle存储过程返回多个记录集
本文转自:http://www.netwinform.com/articleinfo.aspx?id=17 存储过程: CREATE OR REPLACE PROCEDURE p_query_cs ( ...
随机推荐
- 通过模板类简单实现Spark的JobServer
实验前后效果对比: 之前:执行13个节点,耗时16分钟 之后:同样13个节点,耗时3分钟 具体逻辑请参照代码及注释. import java.util.concurrent.{ExecutorServ ...
- poj2192(搜索)
这个题目对于两个字符串A,B是否可以通过规则生成C. import java.util.Scanner; public class Main { public static void main(Str ...
- linux驱动程序之电源管理之linux的电源管理架构(3)
设备电源管理 Copyright (c) 2010 Rafael J. Wysocki<rjw@sisk.pl>, Novell Inc. Copyright (c) 2010 Alan ...
- 【Java基础】继承的一些总结
什么是继承 把一些类的具有共性的东西剥离出来形成一个新的类,然后各个其他类保留自己独有的特性,并用关键字extends继承这个剥离出来的新的类,可以最终达到各类原始相同效果,但是在每个类中,单用一个“ ...
- XXX 用户 is not in the sudoers file. This incident will be reported 的问题解决方案
说的是,这种问题,是出现在ubuntu系统里. root@SparkSingleNode:/usr/local/jdk# pwd /usr/local/jdk root@SparkSingleNode ...
- 数据绑定之DataFormatString
设定BoundField的DataFormatString,通常有以下几种 DataFormatString= "{0:C}" 货币,货币的格式取决于当前Thread中Cultur ...
- 从app里跳到appstore评论页面的实现
// 如果要实现在应用里面跳到appstore的对应评论页面里面的话,只要将下面地址中App_ID替换成自己的id就可以了,其他的地方都不用管. // 如果要用Safari浏览器做实验的话可以将地址中 ...
- 解决libpython2.6.so.1.0: cannot open shared object file
文章解决的问题:安装nginx中需要Python2.6的支持,下面介绍如何安装Python2.6,并建立lib的连接. 问题展示:error while loading shared librarie ...
- C#Winform窗口特效源码(1)
本文基于.Net开发,使用C#作为开发语言,分别包含以下效果: 移动无边框窗口.窗口移动限制(限制在屏幕内).桌面贴边自动隐藏(仿QQ隐藏窗口) 1.移动无边框窗口 采用了消息的方式,可以实现通过窗口 ...
- OceanBase里面的rowkey是什么概念,是由哪些要素构成的?
Rowkey是OceanBase诞生之初就引入的概念,最终被确立是在OceanBase 0.3. 为了便于理解,不妨把OceanBase想象成一个Key-Value系统,Rowkey就是Key,Val ...