ADO.NET Entity Framework 4.0 has an friendly method for loading related objects in a explicit way by using "Include"
Below is an example from MVC Music Store.
| 1 | public ActionResult Browse(string genre) |
| 2 | { |
| 3 | // Retrieve Genre and its Associated Albums from database |
| 4 | var genreModel = storeDB.Genres.Include("Albums") |
| 5 | .Single(g => g.Name == genre); |
| 6 | |
| 7 | return View(genreModel); |
| 8 | } |
In this method, Generes and Albums are related so EF provides an easy way to get both in a single call.
But the main topic of this post is how to achieve this behavior if you are using Telerik OpenAccess ORM.
See the code below -
| 1 | public ActionResult Browse(string genre) |
| 2 | { |
| 3 | // Retrieve Genre and its Associated Albums from database |
| 4 | FetchStrategy fetchStrategy = new FetchStrategy(); |
| 5 | fetchStrategy.LoadWith<Genre>(g => g.Albums); |
| 6 | storeDB.FetchStrategy = fetchStrategy; |
| 7 | |
| 8 | Genre genreModel = storeDB.Genres.FirstOrDefault(g => g.Name == genre); |
| 9 | |
| 10 | return View(genreModel); |
| 11 | } |
You will need a "FetchStargegy" and Load Genre with Albums while getting the results.
Hope this helps.
Cheers
Venkata