namespace EFDemo.Model{// Code first relies on a programming pattern // referred to as convention over configuration. // What this means is that if you want to use code first, // your classes need to follow the certain conventions // while defining schema. // This allows EF to infer the schema that it needs to // create to get the job done.publicclass Project {// Code First has to be told that // this as the primary key column [Key]publicint UniqueProjectIdentifier { get; set; }// this becomes a non-nullable column [Required]publicstring Name { get; set; }// references the complex type as part of the // project object in the databasepublic ProjectDetails Details { get; set; }// list of tasks for a projectpublicvirtual List<Task> Tasks { get; set; }

Lazy loading of the tasks for a project can be turned off by removing the virtual keyword for that navigation property.

publicclass Project{// Code First has to be told that // this as the primary key column [Key]publicint UniqueProjectIdentifier { get; set; }// this becomes a non-nullable column [Required]publicstring Name { get; set; }// references the complex type as part of the // project object in the databasepublic ProjectDetails Details { get; set; }// list of tasks for a projectpublic List<Task> Tasks { get; set; }

In the Project definition above, lazy loading for Tasks has been disabled by removing the virtual keyword for Tasks. Of course, loading of Tasks can still be accomplished using the eager loading constructs or by using an explicit Load().

Question 2: How can you disable lazy loading for all entities?

Lazy loading can be turned off for all entities in the context by setting a flag on the Configuration property. This can be defined in the context: