RIA 服务 - 调用存储过程

发布于 2024-09-19 23:49:56 字数 487 浏览 3 评论 0原文

我正在将 RIA 服务与 Silverlight 和实体框架结合使用。我想调用存储过程并将结果映射到数据网格。最好的方法是什么?存储过程的输出不映射到任何表设计。

我找到了以下文章 -

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

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

无论如何,我对任何和所有解决方案持开放态度。

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.

I found the following article -

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

默嘫て 2024-09-26 23:49:56

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

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

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

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

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

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

4) 将Domain Service 类添加到Web 项目中。选择您刚刚创建的 DataModel 作为此服务的 DataContext。选择您想要向客户端公开的所有实体。应该为这些实体生成服务功能。

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

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

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

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

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

[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。您可以像对待任何其他实体(映射到表的实体)类一样对待它。

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

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

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

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) 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.

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) 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) 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();            
    }

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:

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) 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.

久夏青 2024-09-26 23:49:56

好吧,我知道怎么做了,虽然有点混乱。您需要在域元数据文件中为结果集创建元数据类。之后,RIA 基本上会像对待实体一样对待它。

完整的详细信息可以在这里找到 - http://leeontech。 wordpress.com/2010/05/24/ria-services-and-storedprocedures/

Well, I figured out how to do it, though it's a bit messy. You need to create a metadata class for the result set in the domain metadata file. After that, RIA will treat it essentially like it does an entity.

Full details can be found here - http://leeontech.wordpress.com/2010/05/24/ria-services-and-storedprocedures/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文