在 Silverlight 域服务中包含子实体集合

发布于 2024-12-29 16:09:56 字数 1553 浏览 0 评论 0原文

我有一个域服务类,如下所示

[MetadataTypeAttribute(typeof(Question.QuestionMetadata))]
public partial class Question
{     
    internal sealed class QuestionMetadata
    {
        private QuestionMetadata()
        {
        }

        [Include]
        public EntityCollection<Answer> Answers { get; set; }

        public EntityCollection<AssignmentsQuestionsMapping> AssignmentsQuestionsMappings { get; set; }

        public int Marks { get; set; }

        public string QuestionDescription { get; set; }

        public long QuestionID { get; set; }

        public string QuestionTitle { get; set; }

        public EntityCollection<UserQuestionAnsweredMapping> UserQuestionAnsweredMappings { get; set; }
    }
}

并且我在域服务中有以下查询

public IQueryable<Question> GetQuestionsByAssignmentId(long assignmentId)
        {
            var questions = from q in this.ObjectContext.Questions.Include("Answers")
                            join qam in this.ObjectContext.AssignmentsQuestionsMappings on q.QuestionID equals qam.QuestionID
                            join assign in this.ObjectContext.Assignments on qam.AssignmentID equals assign.AssignmentID
                            where assign.AssignmentID == assignmentId
                            select q;

            return questions;
        }

据我所知,如果您想在域服务查询中包含子实体,那么您可以在实体的元数据文件中设置 [Include] 属性并将其包含在通过 .Include("ChildEntityCollectionName") 查询。

我已经完成了这两个操作,但我仍然没有在客户端收到 ChildEntity Collection。 我缺少什么?

I have a Domain Service class as below

[MetadataTypeAttribute(typeof(Question.QuestionMetadata))]
public partial class Question
{     
    internal sealed class QuestionMetadata
    {
        private QuestionMetadata()
        {
        }

        [Include]
        public EntityCollection<Answer> Answers { get; set; }

        public EntityCollection<AssignmentsQuestionsMapping> AssignmentsQuestionsMappings { get; set; }

        public int Marks { get; set; }

        public string QuestionDescription { get; set; }

        public long QuestionID { get; set; }

        public string QuestionTitle { get; set; }

        public EntityCollection<UserQuestionAnsweredMapping> UserQuestionAnsweredMappings { get; set; }
    }
}

And I have following query in Domain Service

public IQueryable<Question> GetQuestionsByAssignmentId(long assignmentId)
        {
            var questions = from q in this.ObjectContext.Questions.Include("Answers")
                            join qam in this.ObjectContext.AssignmentsQuestionsMappings on q.QuestionID equals qam.QuestionID
                            join assign in this.ObjectContext.Assignments on qam.AssignmentID equals assign.AssignmentID
                            where assign.AssignmentID == assignmentId
                            select q;

            return questions;
        }

As far as I know if you want to include child entity in domain service query, then you have Set [Include] attribute in metadata file for entity and include it in query by .Include("ChildEntityCollectionName").

I have done both of them, but still I am not receiving ChildEntity Collection at my client side.
What am I missing ??

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

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

发布评论

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

评论(1

听你说爱我 2025-01-05 16:09:56

你快到了。您需要添加关联属性来帮助 WCF RIA 了解 QuestionAnswer 是如何关联的。

[Include]
[Association("Question_Answer", "QuestionID", "ParentQuestionID", IsForeginKey=false)]
public EntityCollection<Answer> Answers { get; set; }

这假设您的实体共享外键。

public class Question
{
...
    [Key]
    public long QuestionID { get; set;}
...
}

public class Answer
{
...
    [Key]
    public long AnswerID { get; set;}

    public long ParentQuestionID { get; set;}
...
}

您可以通过RIA服务查看更多信息:插入多个演示模型对象

You are almost there. You need to add an association attribute to help WCF RIA understand how Question and Answer are related.

[Include]
[Association("Question_Answer", "QuestionID", "ParentQuestionID", IsForeginKey=false)]
public EntityCollection<Answer> Answers { get; set; }

This assumes your entities share a foreign key.

public class Question
{
...
    [Key]
    public long QuestionID { get; set;}
...
}

public class Answer
{
...
    [Key]
    public long AnswerID { get; set;}

    public long ParentQuestionID { get; set;}
...
}

You can see more information with RIA Services: Inserting multiple presentation-model objects

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