none
Load a list within another list within an object RRS feed

  • Question

  • I am using Entity Framework 5 with Repository Pattern in my MVC Application.

    I have 3 classes:

    1. Reservation
    2. Guests
    3. Questions

    Each Registration consists of a list of Guests and each guest consists of a list of questions.

    I would like to load a registration that will load Guests and the questions together in a single call... Is that possible? 

    Thanks,

    Moneka



    • Edited by Moneka Wednesday, September 18, 2013 7:07 PM
    Wednesday, September 18, 2013 6:47 PM

Answers

  • You can use the Include method to load child entities.  Here's a MSDN link on the subject:

    http://msdn.microsoft.com/en-us/data/jj574232.aspx

    • Marked as answer by Moneka Friday, September 20, 2013 7:35 PM
    Wednesday, September 18, 2013 8:29 PM
  • Hi Moneka,

    I agree with Cyberhornet. And there is another way to load these associated data.

    You could use Fluent API to define relationships between three POCO classes. After you correctly define the relationships, you could get all the associated data automatically like this code “var reg = context.Registrations.FirstOrDefault();”. The feature is called “Lazy loading”, the reference Cyberhornet has provided.

    Here is some code for you to try:

    public class Registration

        {

            [Key]

            [Required]

            [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]

            public int Id { get; set; }

            public string RName { get; set; }

            public virtual ICollection<Guest> Guests { get; set; }

        }

        public class Guest

        {

            [Key]

            [Required]

            [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]

            public int Id { get; set; }

            public string GName { get; set; }

            public virtual ICollection<Question> Questions { get; set; }

            public virtual Registration Registration { get; set; }

        }

        public class Question

        {

            [Key]

            [Required]

            [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]

            public int Id { get; set; }

            public string QName { get; set; }

            public virtual Guest Guest { get; set; }

    }

    public class TaskDbContext : DbContext

        {

            public DbSet<Question> Questions { get; set; }

            public DbSet<Guest> Guests { get; set; }

            public DbSet<Registration> Registrations { get; set; }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)

            {

                modelBuilder.Entity<Registration>().HasMany(r => r.Guests).WithRequired(g => g.Registration);

                modelBuilder.Entity<Guest>().HasMany(g => g.Questions).WithRequired(q => q.Guest);

            }

        }

    Hope useful to you.

    Best Regards,


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Moneka Friday, September 20, 2013 7:35 PM
    Thursday, September 19, 2013 2:31 AM
    Moderator

All replies

  • You can use the Include method to load child entities.  Here's a MSDN link on the subject:

    http://msdn.microsoft.com/en-us/data/jj574232.aspx

    • Marked as answer by Moneka Friday, September 20, 2013 7:35 PM
    Wednesday, September 18, 2013 8:29 PM
  • Hi Moneka,

    I agree with Cyberhornet. And there is another way to load these associated data.

    You could use Fluent API to define relationships between three POCO classes. After you correctly define the relationships, you could get all the associated data automatically like this code “var reg = context.Registrations.FirstOrDefault();”. The feature is called “Lazy loading”, the reference Cyberhornet has provided.

    Here is some code for you to try:

    public class Registration

        {

            [Key]

            [Required]

            [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]

            public int Id { get; set; }

            public string RName { get; set; }

            public virtual ICollection<Guest> Guests { get; set; }

        }

        public class Guest

        {

            [Key]

            [Required]

            [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]

            public int Id { get; set; }

            public string GName { get; set; }

            public virtual ICollection<Question> Questions { get; set; }

            public virtual Registration Registration { get; set; }

        }

        public class Question

        {

            [Key]

            [Required]

            [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]

            public int Id { get; set; }

            public string QName { get; set; }

            public virtual Guest Guest { get; set; }

    }

    public class TaskDbContext : DbContext

        {

            public DbSet<Question> Questions { get; set; }

            public DbSet<Guest> Guests { get; set; }

            public DbSet<Registration> Registrations { get; set; }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)

            {

                modelBuilder.Entity<Registration>().HasMany(r => r.Guests).WithRequired(g => g.Registration);

                modelBuilder.Entity<Guest>().HasMany(g => g.Questions).WithRequired(q => q.Guest);

            }

        }

    Hope useful to you.

    Best Regards,


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Moneka Friday, September 20, 2013 7:35 PM
    Thursday, September 19, 2013 2:31 AM
    Moderator
  • Hi, 

    I've been using Fluent API. Just having trouble loading list within another list.

    For e.g.

    var test = Registration.Guests.Questions; - it's not possible to do that as there are many guests and each guest could have many questions.

    It is possible doing something like 

    .Include(x => x.Guests.Select(y => y.Questions)

    Thanks,

    Moneka


    • Edited by Moneka Friday, September 20, 2013 7:37 PM
    Friday, September 20, 2013 3:35 PM