如何通过 RIA 服务获取实体及其子实体

发布于 2024-09-30 12:57:16 字数 798 浏览 1 评论 0原文

我对 EF 和 RIA 有点陌生,所以我不确定这是否应该起作用。

我在这里显示了一个简单的模型:

alt text

我添加了 实体框架的 POCO 模板 一切都已连接,因为我可以获得延迟加载、更改通知和关系修复...(我真的希望您仍在阅读)

问题是,在服务器上时,我可以通过简单地调用 comp.SubComponents 来加载组件的子组件。但是,由于某种原因,我在客户端没有该功能...来自 RIAServices.web.g.cs (生成的代码)的我的 Component 类确实如此没有 SubComponent 列表。

是这样的吗?我应该在 RIA 服务上提供此功能吗?

public IEnumerable<SubComponent> GetSubComponents(int componentId)
{
    return m_ctx.SubComponents
           .Where(x => x.Component.Id == componentId)
           .OrderBy(x => x.Name);
}

I'm kind of new to EF and RIA so I'm not sure if this is supposed to work or not.

I have a simple model shown here:

alt text

I added the POCO templates for entity framework and everything is wired as it sohould I can get Lazy Loading, Changes Notifications and Relationship Fixup... (I really hope you're still reading)

The thing is that while on the server I can load a Component's SubComponents by simply calling comp.SubComponents. BUT, for some reason I don't have that feature on the client side... My Component class from the RIAServices.web.g.cs (generated code) does not have a list of SubComponent.

Is that how that's suppose to work? Should I have a this function on the RIA Service?

public IEnumerable<SubComponent> GetSubComponents(int componentId)
{
    return m_ctx.SubComponents
           .Where(x => x.Component.Id == componentId)
           .OrderBy(x => x.Name);
}

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

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

发布评论

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

评论(2

薄荷→糖丶微凉 2024-10-07 12:57:16

我对 RIA 服务了解不多,但我怀疑延迟加载是否可以在客户端工作...我认为当您加载 Components 时,您需要急切地加载 SubComponents code>,在将它们发送给客户端之前:

public IEnumerable<Component> GetComponents()
{
    return m_ctx.Components.Include("SubComponents")
           .OrderBy(x => x.Name);
}

I don't know much about RIA services, but I doubt lazy loading can work from the client side... I think you need to load the SubComponents eagerly when you load the Components, before you send them to the client:

public IEnumerable<Component> GetComponents()
{
    return m_ctx.Components.Include("SubComponents")
           .OrderBy(x => x.Name);
}
乖乖公主 2024-10-07 12:57:16

在我的元数据中,我缺少 Componet 元数据 SubComponent 属性中的属性 [Include][Association] 。该类必须如下所示:

[MetadataType(typeof(Component.Metadata))]
public partial class Component
{
    internal sealed class Metadata
    {
        [Key]
        public int Id { get; set; }

        [Include]
        [Association("ComponentSubComponent","Id", "ComponentId")]
        public ICollection<SubComponent> SubComponents { get; set; }

    }
}

我希望它可以帮助某人:)

额外提示:我在添加 [Include] 属性时遇到问题,因为我没有引用正确的程序集。确保添加对 System.ServiceModel.DomainServices.Server.dll 的引用

编辑:我忘了提及我还缺少 ComponentId 属性在子组件实体上

alt text

In my metadata I was missing the attributes [Include] and [Association] in my Componet's metadata SubComponent property. The class has to look like this:

[MetadataType(typeof(Component.Metadata))]
public partial class Component
{
    internal sealed class Metadata
    {
        [Key]
        public int Id { get; set; }

        [Include]
        [Association("ComponentSubComponent","Id", "ComponentId")]
        public ICollection<SubComponent> SubComponents { get; set; }

    }
}

I hope it helps someone :)

extra tip: I was having problems adding the [Include] attribute cause I was not referencing the right assembly. Make sure you add a reference to System.ServiceModel.DomainServices.Server.dll

Edit: I forgot to mention that I was also missing ComponentId property on the SubComponent entity

alt text

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