Page 1 of 1

EF LINQ to PostgreSQL Include

Posted: Tue 23 Jun 2015 22:37
by damon.cognito
As I understand it you should be able to construct an eager laoding statement like:

Code: Select all

    var blog1 = context.Blogs.Where(b => b.Name == "PostgreSQL") 
                        .Include(b => b.Posts).FirstOrDefault();
but I can only get it to work if I use a string (which is obviously not great). I feel I'm missing something obvious here...

Code: Select all

    var blog1 = context.Blogs.Where(b => b.Name == "PostgreSQL") 
                        .Include("Posts").FirstOrDefault();
(EF to PostgreSQL 9.3.4)

Re: EF LINQ to PostgreSQL Include

Posted: Wed 24 Jun 2015 13:51
by Shalex
damon.cognito wrote:As I understand it you should be able to construct an eager laoding statement like:

Code: Select all

    var blog1 = context.Blogs.Where(b => b.Name == "PostgreSQL") 
                        .Include(b => b.Posts).FirstOrDefault();
This functionality is implemented by Microsoft via the DbExtensions methods: https://msdn.microsoft.com/en-us/librar ... 03%29.aspx. Just add the necessary namespace and try again:

Code: Select all

    using System.Data.Entity;
//...
    var blog1 = context.Blogs.Where(b => b.Name == "PostgreSQL") 
                        .Include(b => b.Posts).FirstOrDefault();

Re: EF LINQ to PostgreSQL Include

Posted: Wed 24 Jun 2015 17:27
by damon.cognito
Brilliant thanks - knew I was missing something. Just in case anyone else is looking at this, the include appears to have to be before the Where.

cheers!