0

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 ??

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
Sumit
  • 2,932
  • 6
  • 32
  • 54
  • Do you have a foreign key set up between Questions and Answers in the database? If so, has your Model (ConceptualEntityModel) been updated since this was implemented? – Chris Jan 27 '12 at 17:28
  • yes Answer table has question id as forgein key. The weird thing is when I return this.ObjectContext.Questions.Include("Answers") i.e. without linq in the query I do get the Answers entity in the Question entity at client side. any ideas what is happening?? – Sumit Jan 28 '12 at 05:09
  • Can you run the same query directly in SQL and verify you are getting results? You need to verify the data in the database conforms to your assumptions in your LINQ. – Ed Chapel Jan 30 '12 at 17:28
  • I did ran the query on Database and it provides me result. Also as I stated before, I am getting questions list but not the answers within the questions which I am trying to include. – Sumit Jan 31 '12 at 04:25

1 Answers1

1

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

Community
  • 1
  • 1
Ed Chapel
  • 6,842
  • 3
  • 30
  • 44
  • I thought if there is association in your Database then there is no need to provide explicit association in the code. Also, The weird thing is when I return this.ObjectContext.Questions.Include("Answers") i.e. without linq in the query I do get the Answers entity in the Question entity at client side. any ideas what is happening?? – Sumit Jan 28 '12 at 05:14