Ado.Net Entity Framework with Many to Many Relationships and Eager Load

I've switched a relationship in a model from one to many to many to many and its effecting how the ObjectQuery.Include method is eager loading objects from the database.

An example Module before adding a many to many looks like:

Company -(1..*)-> Cars -(1..*)-> Passengers
Company -(1..*)-> Passengers

A simple query such as:

company = context.Company.Include("Cars").Include("Passengers").First(e => e.CompanyId = companyId);

both populates all the passengers under the company, but also all the passengers under the cars. I.e. company.Cars[0].Passengers is populated.

But when the model is changed to:
  Company -(1..*)-> Cars -(*..*)-> Passengers
  Company -(1..*)-> Passengers

performing the same query doesn't populate the passengers under the cars.

Adding an additional Include("Cars.Passengers") corrects this.

I initially thought this was a bug, but it makes complete sense when you realise that before the entity framework had enough information to construct the graph. It will query all cars and passengers associated with the company, and each passenger row/object references the car it belonged to, therefor it can assign passengers to cars.

With a many to many relationship added there is an additional link table that will not query unless told to. Without the relationship between cars and passengers the framework cannot reconstruct the graph.

Comments

# re: Ado.Net Entity Framework with Many to Many Relationships and Eager Load
Gravatar The .NET Micro Framework is a development and execution environment for resource-constrained devices.Micro Framework can run on bare hardware and in extreme memory/size conditions.
It's far more awesome than .Net Compact Framework.
Left by dossiers sur casinos en ligne on 4/9/2010 11:59 PM

Leave Your Comment

Title*
Name*
Email (never displayed)
 (will show your gravatar)
Url
Comment*

Please add 2 and 4 and type the answer here:

Preview Your Comment.