Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child Entities not loaded when query joins tables

I have a many-to-many relationship where Content has ContentTags which point to a Tag. I've put the relevant [Include] attributes on my entities to create the properties.

If I write enumerate ObjectContext.Contents.Include("ContentTags.Tag") then I get the ContentTags and Tags included as expected. When I use a join however the ContentTags are missing from my Content entity:

var contentsForTag =
    from c in ObjectContext.Contents.Include("ContentTags.Tag")
    join ct in ObjectContext.ContentTags on c.Id equals ct.ContentId
    join t in ObjectContext.Tags on ct.TagId equals t.Id
    where t.Name.ToLower().Contains(lowerTag)
    select c;

Any ideas what's going on here?

like image 953
Phill Avatar asked Dec 10 '25 13:12

Phill


1 Answers

I'm not sure why this is happening but I think it is because of a contradiction. The join says that EF should load only the tags that containes lowerTag but the Include says that all tags should be loaded. I would guess EF can't resolve this and that is why none are included. You should be able to write your query without the join though

var contentsForTag =
    from c in ObjectContext.Contents.Include("ContentTags.Tag")
    where c.ContentTags.Any(ct => ct.Tag.Name.ToLower().Contains(lowerTag))
    select c;
like image 125
Mikael Eliasson Avatar answered Dec 12 '25 01:12

Mikael Eliasson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!