RIA服务-调用存储过程 [英] RIA Services - call a stored procedure

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

问题描述

我正在将RIA Services与Silverlight和Entity Framework结合使用。我想调用存储过程并将结果映射到datagrid。做这个的最好方式是什么?存储过程的输出未映射到任何表设计。

I am using RIA Services with Silverlight and Entity Framework. I want to call a stored procedure and map the results to a datagrid. What is the best way to do this? The output of the stored procedure doesn't map to any table design.

我找到了以下文章-

http://blogs.msdn.com/b/tom/archive/2009/05/07/silverlight-ria-calling-stored-procedures-that-don-t-return-tables.aspx

但是,它对我不起作用-我收到一条错误消息,指出结果复杂集未定义主键。我看不到如何在代码中定义它。

However, it doesn't work for me - I get an error saying that the result complex set does not have a primary key defined. I can't see how to define this in code.

无论如何,我愿意接受所有解决方案。

Anyway, I'm open to any and all solutions.

推荐答案

我在此站点上找到了以下出色的分步指南-

I found the following excellent step-by-step guide at this site -

http://betaforums.silverlight.net/forums/p/ 218383 / 521023.aspx

1)将ADO实体数据模型添加到您的Web项目中;选择从数据库生成选项;选择要连接的数据库实例。

1) Add a ADO Entity Data Model to your Web project; Select generate from database option; Select your Database instance to connect to.

2)选择要导入到模型的数据库对象。您可以展开表节点以选择要导入到模型的任何表。展开存储过程节点以选择您的存储过程。单击完成完成导入。

2) Choose your DB object to import to the Model. You can expand Table node to select any table you want to import to the Model. Expand Stored Procedure node to select your Stored Precedure as well. Click Finish to finish the import.

3)右键单击数据库模型设计器,选择添加/函数导入。给函数命名(与SP相同的名称就可以了),然后选择要映射的存储过程。如果您的SP仅返回一个字段,则可以将返回结果映射到标量集合。如果您的SP返回多个字段,则可以将返回结果映射到集合或实体(如果所有字段都来自单个表)或复杂类型的集合。

3) Right click the DB model designer to select Add/Function Import. Give the function a name (same name as your SP would be fine) and select the Stored Procedure you want to map. If your SP returns only one field, you can map the return result to a collection of scalars. If your SP returns more than one field, you could either map the return result to a collection or Entity (if all the field are from a single table) or a collection of Complex types.

如果要使用复杂类型,可以单击获取列按钮以获取SP的所有列。然后单击创建新的复杂类型按钮以创建此复杂类型。

If you want to use Complex type, you can click Get Column button to get all the columns for your SP. Then click Create new Complex type button to create this Complex type.

4)将域服务类添加到Web项目中。选择您刚刚创建的DataModel作为此服务的DataContext。选择要暴露给客户端的所有实体。应该为那些实体生成服务功能。

4) Add a Domain Service class to the Web project. Select the DataModel you just created as the DataContext of this Service. Select all the entitis you want expose to the client. The service functions should be generated for those entities.

5)在实体列表中可能看不到复杂类型。您必须在服务中手动为SP添加查询功能:
假设您的SP称为SP1,生成的复杂类型称为SP1_Result。

5) You may not see the Complex type in the Entity list. You have to manully add a query function for your SP in your Service: Say your SP is called SP1, the Complex type you generated is called SP1_Result.

在域服务类中添加以下代码:

Add the following code in your Domain Service class:

public IQueryable<SP1_Result> SP1()
    {
        return this.ObjectContext.SP1().AsQueryable();            
    }

现在您可以编译项目了。您可能会收到这样的错误: SP1_Result没有密钥(如果您不在RIA服务SP1 beta中)。如果这样做,则需要在服务元数据文件中执行以下操作:

Now you can compile your project. You might get an error like this: "SP1_Result does not have a Key" (if you not on RIA service SP1 beta). If you do, you need to do the following in the service metadata file:

添加了SP1_Result元数据类并标记了键字段:

Added a SP1_Result metadata class and tagged the Key field:

[MetadataTypeAttribute(typeof(SP1_Result.SP1_ResultMetadata))]
public partial class SP1_Result
{
    internal sealed class SP1_ResultMetadata
    {
        [Key]
        public int MyId;  // Change MyId to the ID field of your SP_Result
    }
} 

6)编译您的解决方案。现在,您已经向客户端公开了SP1_Result。检查生成的文件,您应该看到SP1_Result作为实体类生成。现在,您可以在Silverlight代码中访问DomainContext.SP1Query和DomainContext.SP1_Results。您可以像对待其他任何Entity(映射到表的实体)类一样对待它。

6) Compile your solution. Now you have SP1_Result exposed to the client. Check the generated file, you should see SP1_Result is generated as an Entity class. Now you can access DomainContext.SP1Query and DomainContext.SP1_Results in your Silverlight code. You can treat it as you do with any other Entity(the entity mapped to a table) class.

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

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