none
DbContext not returning child items RRS feed

  • Question

  • I've created a code first silverlight 4 ria application. When a run the application the child items are only sometimes being returned. What i am doing wrong?

    Here's the code:

       public class MyDomainContext : DbContext
        {
            [Include]
            public DbSet<Solution> Solutions { get; set; }
          
            [Include]
            public DbSet<SolutionVersion> SolutionVersions { get; set; }
    
            // SolutionVersion has child items too...
        }
    
        public partial class MyDomainService : DbDomainService<MyDomainContext >
        {
            public IQueryable<Solution> GetAllSolutions()
            {
                return DbContext.Solutions.
                    Include(p => p.SolutionVersions);
            }
    
            public IQueryable<SolutionVersion> GetSolutionVersions(Guid solutionId)
            {
                return DbContext.SolutionVersions.Where(p => p.SolutionId == solutionId).Include(p => p.Projects);
            }
       }
    
        // And in my silverlight view model  I'm calling it with the following code
        public class MyViewModel : ViewModelBase
        {
    
            private MyViewModel ()
            {
                SolutionVms = new ObservableCollection<SolutionViewModel>();
    
                var context = new WeaverDashboardDomainContext();
                DbContext.Load(DbContext.GetAllSolutionsQuery()).Completed += (s, e) =>
                {
                    DbContext.Solutions.ForEach(p =>
                    {
        
                        SolutionVms.Add(new SolutionViewModel(p));
                    });
                };
    	}
    
    	public ObservableCollection<SolutionViewModel> SolutionVms {get;Private set;}
        }
    
        public class SolutionViewModel: ViewModelBase
        {
    
            private SolutionViewModel(SolutionVersion solVersion)
            {
                SolutionVersionVms  = new ObservableCollection<SolutionViewModel>();
    	    SolutionVersionVms.Add(new SOlutionVersionViewModel(solVersion);
    	}
    
    	public ObservableCollection<SolutionVersionViewModel> SolutionVersionVms {get;Private set;}
        }
    

     

    Anybody any ideas?

     

    Thanks in advance..

    MK

     

     

    Monday, February 6, 2012 9:33 AM

Answers

  • Ok finally figured this out so thought i'd put the solution here to stop other people having the same torture as i did :

    1. In your domain service make sure you add the include method:

            public IQueryable<solution> GetAllSolutions()
            {
                return DbContext.Solutions.
                    Include(p => p.SolutionVersions);
                // The following is also acceptable and preferable when adding granchildren.
                 //Include("SolutionVersions.[grandchild].[greatgrandchild]") ;
            }</solution>

    2. Now here's the gotcha:

       [MetadataTypeAttribute(typeof(Solution.Solution_Metadata))]
        public partial class Solution
        {
            internal sealed class Solution_Metadata
            {
                private Solution_Metadata() { }
    
                [Include]
                public EntityCollection<solutionversions> SolutionVersions;
            }
        }</solutionversions>
    This can be placed within the poco itself but i prefer to place it in a partial class for several reasons which i won't go into here.

    And thats it.

    Cheers.

    One slightly happier coder :)


    • Marked as answer by Kelbob Tuesday, February 7, 2012 11:25 AM
    • Edited by Kelbob Tuesday, February 7, 2012 3:54 PM
    Tuesday, February 7, 2012 11:25 AM

All replies

  • Hi MK,

    Welcome!

    To Load related entities, you can refer the link here:

    http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx

    According to your description, your problem is random. You can try to watch the "T-SQL" if it is expected for LINQ query in SQL Profiler

    BTW, you should repost your question here: http://forums.silverlight.net/53.aspx, thanks for understanding.

    Have a nice day. 


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, February 7, 2012 6:35 AM
    Moderator
  • Ok finally figured this out so thought i'd put the solution here to stop other people having the same torture as i did :

    1. In your domain service make sure you add the include method:

            public IQueryable<solution> GetAllSolutions()
            {
                return DbContext.Solutions.
                    Include(p => p.SolutionVersions);
                // The following is also acceptable and preferable when adding granchildren.
                 //Include("SolutionVersions.[grandchild].[greatgrandchild]") ;
            }</solution>

    2. Now here's the gotcha:

       [MetadataTypeAttribute(typeof(Solution.Solution_Metadata))]
        public partial class Solution
        {
            internal sealed class Solution_Metadata
            {
                private Solution_Metadata() { }
    
                [Include]
                public EntityCollection<solutionversions> SolutionVersions;
            }
        }</solutionversions>
    This can be placed within the poco itself but i prefer to place it in a partial class for several reasons which i won't go into here.

    And thats it.

    Cheers.

    One slightly happier coder :)


    • Marked as answer by Kelbob Tuesday, February 7, 2012 11:25 AM
    • Edited by Kelbob Tuesday, February 7, 2012 3:54 PM
    Tuesday, February 7, 2012 11:25 AM