As mentioned in comments I'm fairly confident (though my usage of EF Core is limited) that this can't be done automatically through standard mapping of navigation properties. Also using .Load
is less efficient (and much longer) than using .Include
as you'll be running multiple queries just like using the lazy-loading navigation property itself.
There are 2 ways I can think of to approach the problem of not wishing to do the checks every time as per your question. The first would work for loading your Blog
entity specifically, and the second that you could reuse across others but still requires changes to your loading.
1; Extension Method to load all related entities, something like this:
public static IQueryable<Blog> EagerWhere(this DbSet<Blogs> dbset, Expression<Func<Blog, bool>> expr) {
return dbset
.Include(m => m.Posts)
.Include(m => m.Author)
.Where(expr);
}
// .. usage
db.Blogs.EagerWhere(m => m.Id == 1);
(Note that I wrote this as EagerWhere
so you have more control over the query so it's more reusable rather than a simple Find
replacement)
2; Write a custom attribute, and write a method used similar to the above that performs the same action as .Find(..)
but uses Reflection to find the properties with your custom attribute, and passes them into .Include(..)
recursively.
I won't write an example for that as I'm not sure it's worth the effort due to the (normally) limited amount of entities created.