I would like to know how to load multiple level of entities; more specifically how to load multiple level of descendants of a single entity. Here is an example I created. Su
I'm working on EF4.1 project at the moment. I don't need to specify anything - child objects get loaded automatically. I just need to invoke a property on that object.
E.g.: customerObj.Address.County
Ensure EagerLoading (not LazyLoading) is enabled.
http://blogs.microsoft.co.il/blogs/gilf/archive/2010/06/22/eager-loading-with-repository-pattern-and-entity-framework.aspx
If you are SHOWING data like this, you need to ensure this loading is all done inside of your ObjectContext. If you are saving data only, you could lazy load as long as you are using the data in the same ObjectContext.
Edit: Since you want to load all the related entities on the model you send in, try simply loading your entity as mentioned above, and then call TryUpdateModel(yourLoadedModel) to merge the values from ModelState with your loaded object and save it. Now you have all relationships as well as the values from the form.
See Eagerly Loading Multiple Levels of Entities and Explicitly loading related entities in http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx
You can get it like this
context.Entry(product).Reference(p => p.Category).Load();
string someString1 = product.Category.SomeProperty;
...
context.Entry(product.Category).Reference(c => c.CategoryType).Load();
string someString2 = product.Category.CategoryType.Name;