Been having a play about with ef core and been having an issue with the include statement. For this code I get 2 companies which is what i expected.
Lazy loading is not yet possible with EF Core. Refer here.
Alternatively you can use eager loading.
Read this article
Below is the extension method i have created to achieve the eager loading.
Extension Method:
public static IQueryable IncludeMultiple(
this IQueryable source,
List>> navigationPropertyPath) where TEntity : class
{
foreach (var navExpression in navigationPropertyPath)
{
source= source.Include(navExpression);
}
return source.AsQueryable();
}
Repository Call:
public async Task FindOne(ISpecification spec)
{
return await Task.Run(() => Context.Set().AsQueryable().IncludeMultiple(spec.IncludeExpression()).Where(spec.IsSatisfiedBy).FirstOrDefault());
}
Usage:
List
Dependencies:
public class BlogSpec : SpecificationBase
{
readonly int _blogId;
private readonly List _nestedObjects;
public ChallengeSpec(int blogid, List nestedObjects)
{
this._blogId = blogid;
_nestedObjects = nestedObjects;
}
public override Expression> SpecExpression
{
get { return blogSpec => blogSpec.Id == this._blogId; }
}
public override List>> IncludeExpression()
{
List>> tobeIncluded = new List>>();
if (_nestedObjects != null)
foreach (var nestedObject in _nestedObjects)
{
if (nestedObject is Rules)
{
Expression> expr = blog => blog.Rules;
tobeIncluded.Add(expr);
}
}
return tobeIncluded;
}
}
Will be glad if it helps. Please note this is not a production ready code.