1 min read

Entity Framework: Part 2

I have been reading a bit of the documentation about the ADO.NET Entity Framework. There are a few passages that relate to my previous post:

Because referenced objects are not automatically loaded, you must call the Load method on the entity reference to load the related object data into the object context. You can also specify a query path that defines which related objects to load with returned objects. For more information, see Querying Data as Objects (Entity Framework).



A query path definition ensures that the Course objects related to Department objects are also returned.

The code accompanying this is:

// Define a query that returns all Department objects and related// Course objects, ordered by name. ObjectQuery departmentQuery = schoolContext.Department.Include(“Course”).OrderBy(“it.Name”);

Another difference between LINQ to SQL and using the Entity Framework is that the SubmitChanges() method’s equivalent is the SaveChanges() method.

So I managed to solve my original problem of not being able to follow references in related tables/objects by using the Include method when performing my queries:

My first test method from the last post, TestFooHasTwoBars() will pass when it’s written like this (new code is highlighted):

[TestMethod] publicvoid TestFooHasTwoBars() { testlinqEntities entities = new testlinqEntities(); Foo foo = (from f in entities.Foo.Include(“Bar”)where f.FooId == 1 select f).First(); Assert.IsNotNull(foo); Assert.AreEqual(2, foo.Bar.Count); }

I’m not sure if there’s a way to do that in “pure” LINQ (ie without having to include the string to reference the Bar property.