Expressing and querying entity relations (projections?) #4232
Replies: 2 comments
-
Hi @MartinKuhne , public IQueryable<Author> GetAuthors([Service] ApplicationDbContext dbContext) => dbContext.Authors.Include(x => x.Books); |
Beta Was this translation helpful? Give feedback.
-
This question does not relate to EF Core, as it clearly states two Mongo collections. MongoDB, as a NoSQL database, does not understand/support/enforce foreign key relationships automatically. Even if the EF Core suggestion of using ".Include" was possible, it would be a bad idea, leading to significant over-fetching of data. Do you want to retrieve the list of books every time you fetch an author? The correct way to handle this in HotChocolate is to specify the resolver needed to retrieve the Books field inside the Author object type. Exactly how you do this will depend on the style of code you are following, but essentially will involve writing the MongoDB query to retrieve all Books with a certain AuthorId. (You should also build an index on the Books collection by AuthorId, otherwise this will involve a sequential search of all the Books in the collection.) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
So I have two Mongo collections, let's say Author and Book
and I pretty much have the boilerplate query
and the same for Book
But when I say
books is null
How is the linkage expressed for the relation? How do I make it get SELECT * FROM BOOKS WHERE Id = AuthorId? I feel like I am missing some fundamental concept here. Like in entity framework, there was an implicit contract where as long as you had Id and AuthorId properties, it would recognize the foreign key relationship.
TIA
Martin
Beta Was this translation helpful? Give feedback.
All reactions