Reputation: 195
Consider following LINQ query:
var item = (from obj in _db.SampleEntity.Include(s => s.NavProp1)
select new
{
ItemProp1 = obj,
ItemProp2 = obj.NavProp2.Any(n => n.Active)
}).SingleOrDefault();
This runs as expected, but item.ItemProp1.NavProp1
is NULL.
As it explains here this is because of the query actually changes after using Include()
. but the question is what is the solution with this situation?
When I change the query like this, every things works fine:
var item = (from obj in _db.SampleEntity.Include(s => s.NavProp1)
select obj).SingleOrDefault();
Regarding to this article I guess what the problem is... but the solution provided by author not working in my situation (because of using anonymous type in final select rather than entity type).
Upvotes: 13
Views: 32274
Reputation: 5003
Since this is the first result coming up in web searches, then I want to make sure this answer is also included here. Note that the accepted answer here also worked for me, but this is much easier and seems more correct--at least for simple queries using EF Core:
https://stackoverflow.com/a/71362717/1467396
Make sure you are using "Microsoft.EntityFrameworkCore", NOT "System.Data.Entity"
Upvotes: 0
Reputation: 109252
As you mentioned, Include
is only effective when the final result of the query consists of the entities that should include the Include
-d navigation properties.
So in this case Include
has effect:
var list = _db.SampleEntity.Include(s => s.NavProp1).ToList();
The SQL query will contain a JOIN
and each SampleEntity
will have its NavProp1
loaded.
In this case it has no effect:
var list = _db.SampleEntity.Include(s => s.NavProp1)
.Select(s => new { s })
.ToList();
The SQL query won't even contain a JOIN
, EF completely ignores the Include
.
If in the latter query you want the SampleEntity
s to contain their NavProp1
s you can do:
var list = _db.SampleEntity
.Select(s => new { s, s.NavProp1 })
.ToList();
Now Entity Framework has fetched SampleEntity
s and NavProp1
entities from the database separately, but it glues them together by a process called relationship fixup. As you see, the Include
is not necessary to make this happen.
However, if Navprop1
is a collection, you'll notice that...
var navprop1 = list.First().s.Navprop1;
...will still execute a query to fetch Navprop1
by lazy loading. Why is that?
While relationship fixup does fill Navprop1
properties, it doesn't mark them as loaded. This only happens when Include
loaded the properties. So now we have SampleEntity
all having their Navprop1
s, but you can't access them without triggering lazy loading. The only thing you can do to prevent this is
_db.Configuration.LazyLoadingEnabled = false;
var navprop1 = list.First().s.Navprop1;
(or by preventing lazy loading by disabling proxy creation or by not making Navprop1
virtual.)
Now you'll get Navprop1
without a new query.
For reference navigation properties this doesn't apply, lazy loading isn't triggered when it's enabled.
In Entity Framework core, things have changed drastically in this area. A query like _db.SampleEntity.Include(s => s.NavProp1).Select(s => new { s })
will now include NavProp1
in the end result. EF-core is smarter in looking for "Includable" entities in the end result. Therefore, we won't feel inclined to shape a query like Select(s => new { s, s.NavProp1 })
in order to populate the navigation property. Be aware though, that if we use such a query without Include
, lazy loading will still be triggered when s.NavProp1
is accessed.
Upvotes: 66
Reputation: 5307
I know this will probably get a few laughs, but don't forget the obvious like i just did. The row in the database didn't actually have a foreign key reference! I should have checked the dam data first before thinking EF Include wasn't working! Grrr. 30 minutes of my life I won't get back.
Upvotes: 2
Reputation: 1145
How did you find that item.ItemProp1.NavProp1
is null. EF uses proxies to load all required properties when you try to access it.
What about
var item = (from obj in _db.SampleEntity.Include(s => s.NavProp1)
select obj).SingleOrDefault();
Assert.IsNotNull(obj.NavProp1);
Assert.IsNotNull(obj.NavProp2);
You can also try with
var item = (from obj in _db.SampleEntity.Include(s => s.NavProp1)
select new
{
ItemProp1 = obj,
NavProp1 = obj.NavProp1,
ItemProp2 = obj.NavProp2.Any(n => n.Active)
}).SingleOrDefault();
Assert.IsNotNull(item.NavProp1)
Of course I assume that you don't have any problems with EF navigation property mappings.
Upvotes: 0
Reputation: 13391
If your model is defined properly it should work without any problems.
using System.Data.Entity;
var item = _db.SampleEntity
.Include(p => p.NavigationProperty)
.Select(p => new YourModel{
PropertyOne = p.Something,
PropertyTwo = p.NavigationProperty.Any(x => x.Active)
})
.SingleOrDefault(p => p.Something == true);
Upvotes: 1