RIA 服务 SP2 函数复杂类型在对象上下文中不可见

发布于 2024-11-06 15:09:25 字数 352 浏览 0 评论 0原文

我正在努力从服务层返回复杂类型。它似乎无法从我的对象上下文访问。

这是服务层的查询。一切编译正常。

    public IQueryable<USP_GetPostsByThreadID_Result> uspGetPostsByThreadID(int ThreadID)
    {
        return this.ObjectContext.USP_GetPostsByThreadID(ThreadID).AsQueryable();
    }  

当我尝试从客户端调用它时,ForumContext 没有看到它。我检查了客户端生成的文件,没有生成任何类似的内容。帮助!!!

I am struggling with returning a complex type from my services layer. It doesnt seem to be accessible from my object context.

This is the query in the service layer. All compiling fine.

    public IQueryable<USP_GetPostsByThreadID_Result> uspGetPostsByThreadID(int ThreadID)
    {
        return this.ObjectContext.USP_GetPostsByThreadID(ThreadID).AsQueryable();
    }  

When I try and call it from my client, the ForumContext is not seeing it. I checked the client generated file and nothing similar is being generated. Help!!!

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

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

发布评论

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

评论(3

女中豪杰 2024-11-13 15:09:25

您的方法的名称可能不符合查询的预期约定。请尝试以下一项或两项操作:

  • 添加 [Query] 属性
  • 将方法重命名为 GetUspPostsByThreadID

结果:

[System.ServiceModel.DomainServices.Server.Query]
public IQueryable<USP_GetPostsByThreadID_Result> GetUspPostsByThreadID(int ThreadID)
{
    return this.ObjectContext.USP_GetPostsByThreadID(ThreadID).AsQueryable();
}

The name of your method may not meet the expected convention for queries. Try one or both of the following:

  • Add the [Query] attribute
  • Rename the method to GetUspPostsByThreadID

Result:

[System.ServiceModel.DomainServices.Server.Query]
public IQueryable<USP_GetPostsByThreadID_Result> GetUspPostsByThreadID(int ThreadID)
{
    return this.ObjectContext.USP_GetPostsByThreadID(ThreadID).AsQueryable();
}
后知后觉 2024-11-13 15:09:25

在此处输入图像描述

存储过程从多个表返回数据是很常见的。返回类型不太适合任何实体类型(表)。因此,如果我们将复杂类型定义为存储过程调用的对象返回集合,那么它对于开发人员来说将成为一个非常强大的工具。

按照这些步骤,我成功地在示例 AdventureWorks 数据库上实现了复杂类型的配置。
1. 参考图片并确保存储过程和函数导入已完成。
2. 添加域服务,将其命名为 AdventureDomainService。
3. 现在是时候定义告诉 RIA 服务框架将我的复杂类型识别为实体类型了。为了能够做到这一点,我们需要识别一个 [Key] DataAnnotation。实体类型为应用程序的数据模型提供数据结构,根据设计,每个实体类型都需要定义唯一的实体键。我们可以在元数据类文件 AdventureDomainService.metadata.cs 中的一个属性或一组属性上定义键,

首先定义类,然后添加 MetadatatypeAttribute,如:


[MetadataTypeAttribute(typeof(CTEmployeeManagers.CTEmployeeManagersMetadata))]
公共部分类 CTEmployeeManagers
{

    internal sealed class CTEmployeeManagersMetadata
    {
        private CTEmployeeManagersMetadata() { }
        [Key]
        public int EmployeeID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int ManagerID { get; set; }
        public string ManagerFirstName { get; set; }
        public string ManagerLastName { get; set; }
    }
}

  1. 定义域服务方法以返回用于填充 Silverlight 网格或任何其他数据消耗控件的对象/实体的集合。

public IQueryable<CTEmployeeManagers> GetEmployeeManagers(int empId)
    {
         return this.ObjectContext.GetEmployeeManagers(empId).AsQueryable();
    }

如果我们要从 SQL 等数据源中获取记录,我们定义 IQueryable,而如果我们要从内存集合、dictionaty、arrays.lists 等中获取记录,我们定义 IEnumerable。

  1. 编译服务器端以生成客户端proxy。

  2. 在 Silverlight 端打开 MainPage.xaml 或放置数据网格的任何位置,然后添加以下命名空间:


: System.ServiceModel.DomainServices.Client;
使用 SLBusinessApplication.Web;
使用 SLBusinessApplication.Web.Services;

..


  1. 加载数据并显示:

publicpartialclassMyPage:Page
{
AdventureDomainContext ctx = new AdventureDomainContext();
公共我的页面()
{
初始化组件();
LoadOperation loadOp = this.ctx.Load(this.ctx.GetEmployeeManagersQuery(29));
myGrid.ItemsSource = loadOp.Entities;

    // Executes when the user navigates to this page.
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {

    }
}

  1. 就是需要做的全部事情。

enter image description here

Its very common to have a stored procedure returning data from multiple tables. The return type doesn't fit well under any of the Entity Types(Tables). Therefore if we define Complex Type as the return collection of objects from Stored Procedure invocation, it becomes quite a powerful tool for the developer.

Following these steps I have achieved successfully the configuration of complex type on a sample AdventureWorks database.
1. Refer the picture and ensure the Stored procedure and function import is done.
2. Add the Domain Service name it as AdventureDomainService.
3. Now its time to define the tell the RIA services framework to identify my Complex Type as Entity Type. To be able to do this, we need to identify a [Key] DataAnnotation. Entity types provide data structure to the application's data model and by design, each entity type is required to define a unique entity key. We can define key on one property or a set of properties in metadata class file AdventureDomainService.metadata.cs

First define the class then add MetadatatypeAttribute like :


[MetadataTypeAttribute(typeof(CTEmployeeManagers.CTEmployeeManagersMetadata))]
public partial class CTEmployeeManagers
{

    internal sealed class CTEmployeeManagersMetadata
    {
        private CTEmployeeManagersMetadata() { }
        [Key]
        public int EmployeeID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int ManagerID { get; set; }
        public string ManagerFirstName { get; set; }
        public string ManagerLastName { get; set; }
    }
}

  1. Define the Domain service method to return the collection of objects/entities for populating the Silverlight Grid or any other data consuming controls.

public IQueryable<CTEmployeeManagers> GetEmployeeManagers(int empId)
    {
         return this.ObjectContext.GetEmployeeManagers(empId).AsQueryable();
    }

We define IQueryable if we are to fetch the records from datasources like SQL, whereas we define IEnumerable if we are to fetch the records from in memory collections,dictionaty,arrays.lists, etc.

  1. Compile the server side to generate the client proxy.

  2. In the Silverlight side open the MainPage.xaml or wherever the datagrid is put, then add following namespaces :


using System.ServiceModel.DomainServices.Client;
using SLBusinessApplication.Web;
using SLBusinessApplication.Web.Services;

..


  1. Load the data and display:

public partial class MyPage : Page
{
AdventureDomainContext ctx = new AdventureDomainContext();
public MyPage()
{
InitializeComponent();
LoadOperation loadOp = this.ctx.Load(this.ctx.GetEmployeeManagersQuery(29));
myGrid.ItemsSource = loadOp.Entities;
}

    // Executes when the user navigates to this page.
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {

    }
}

  1. That is all that is needed to do.
苦行僧 2024-11-13 15:09:25

它必须是实体的一部分。复杂类型不能自行返回

It has to be part of an entity. Complex types cannot be returned by themselves

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