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

时间:2012-01-27 06:07:11

标签: entity-framework silverlight-4.0 include wcf-ria-services

我有一个Domain Service类,如下所示

[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。 我错过了什么?

1 个答案:

答案 0 :(得分:1)

你快到了。您需要添加关联属性以帮助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 Services: Inserting multiple presentation-model objects

查看更多信息