1

Closed

Non-generic IQueryable.Include does not work

description

Hi,

it seems IQueryable.Include(...) does not work for collections when using non-generic DbSet. Eg. dbContext.Set(typeof(Entity)).Include("Collection") will not load items included in the collection.
Closed Mar 11, 2013 at 11:29 PM by ajcvickers
No repro.

comments

RoMiller wrote Jan 18, 2013 at 11:29 PM

EF Team Triage: Assigning for further investigation.

ajcvickers wrote Mar 2, 2013 at 8:58 PM

@djanosik Thanks for reporting this issue. Unfortunately we have been unable to reproduce it--Include for non-generic DbSets seems to work fine in my testing. Could you please provide more information about how to reproduce the problem--for example, a simple model where this fails would be very useful. Also, if you get an exception then please provide the message and full stack trace.

In case it helps, the code I used to attempt to repro the issue is below.
    public class Blog
    {
        public int Id { get; set; }
        public string Title { get; set; }

        public virtual ICollection<Post> Posts { get; set; }
    }

    public class Post
    {
        public int Id { get; set; }
        public string Title { get; set; }

        public int BlogId { get; set; }
        public virtual Blog Blog { get; set; }
    }

    public class BlogContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new BlogContext())
            {
                var blog = new Blog {Title = "One Unicorn"};
                context.Posts.Add(new Post { Title = "Open source FTW1", Blog = blog });
                context.Posts.Add(new Post { Title = "Open source FTW2", Blog = blog });
                context.Posts.Add(new Post { Title = "Open source FTW3", Blog = blog });
                context.SaveChanges();
            }

            using (var context = new BlogContext())
            {
                context.Configuration.LazyLoadingEnabled = false;

                var blogs = context.Set(typeof (Blog)).Include("Posts");

                foreach (Blog blog in blogs)
                {
                    Console.WriteLine("Blog: {0}", blog.Title);
                    foreach (var post in blog.Posts)
                    {
                        Console.WriteLine("  Post: {0}", post.Title);
                    }
                }
            }
        }
    }

ajcvickers wrote Mar 11, 2013 at 11:28 PM

Close as no repro.