Lazy loading is a coding technique that minimizes resource usage and database access by not loading an entity until it is actually accessed. In EF4, lazy loading is switched on by default. If you want to turn lazy loading off, then set the ContextOptions.LazyLoadingEnabled option to false in the context class:
ctx.ContextOptions.LazyLoadingEnabled = false;
While we are looking at lazy loading, it's worth mentioning its evil twin, eager loading. Eager loading is the exact opposite of lazy loading, and means preloading an entity before you need it. This can increase the responsiveness of your application if the initialization is spread out over your application's lifetime. Julia Lerman has a great post that describes a number of ways to implement eager loading and measures the effect it has on performance: http://thedatafarm.com/blog/data-access/the-cost-of-eager-loading-in-entity-framework.
If your entities contain many fields, then it can be useful to subdivide them into types. For example, a Patient entity in a medical system may have a huge amount of properties, but using complex types, we could divide this information up like so:
Patient.Demographic.FirstName Patient.Demographic.Age Patient.Demographic.LastName Patient.Clinical.BloodType Patient.Financial.InsurerName
Previously, if you wanted to accomplish this, it was necessary to manually edit the CSDL, but as of EF4 you can accomplish this in the designer. Let's see how to work with this feature with our Film entity.
Select the Film entity.
Hold down the Ctrl key and select the Description and Length properties (Figure 8-11).
Visual Studio will create a new property called ComplexProperty; rename this property Detail.
If you open Program.cs, you will now be able to access these properties using code similar to the following:
Film Film = new Film(); Film.Detail.Description = "New film"; Film.Detail.Length = 200;
|
The Function Import wizard will now create complex types from stored procedures. For example, let's imagine we wanted to add a method to our Film entity to return information about some of the crew, which is retrieved using the following stored procedure (mocked up for ease of use):
CREATE PROCEDURE FilmGetCrewInfo @filmID int AS SELECT 'James Cameron' as Director, 'Arnold Schwarzenegger' as LeadActor1, 'Linda Hamilton' as LeadActor2
Right-click the newly created complex type and add three new string scalar properties called Director, LeadActor1, and LeadActor2 (Figure 8-12).
Right-click the designer surface and select Add Function Import to bring up the screen shown in Figure 8-13. (I also clicked the Get Column Information button when I completed the other information to populate the stored procedure column information section).
Enter the function import name GetCrewInfo.
Select the stored procedure name FilmGetCrewInfo.
Select Complex in the Returns a Collection Of radio button options, and then FilmCrew on the drop-down (notice how you have the option to create a complex type from the results of the stored procedure).
Click OK. The EF designer will have added this function to the context where it can be accessed as follows (note that you could then move this into your entity using partial classes):
var crew = ctx.GetCrewInfo(1);
18.217.43.228